Announcement

Collapse
No announcement yet.

PwrDev's - class template

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PwrDev's - class template

    PwrDev has a feature called VD_VarClass and is a com object using a variant variable.
    While it has no capabilities to support the PowerBASIC's dispatch object, it is a real com object though.
    It just lacks the IDispatch and it's member>id coding.

    The following is a class module which will be added to PwrDev as part of a small class wizard.

    The following creates an object from Class1.
    The code contains remarks and should help reading:
    Code:
        [color=#007F00]' Use a variant which holds the com-object.[/color]
        [color=#0000FF]Local[/color] v [color=#0000FF]As[/color] [color=#0000FF]Variant[/color]
    
        [color=#007F00]' Create the object from class1[/color]
        [color=#0000FF]If[/color] Class1_Create( v ) [color=#0000FF]Then[/color]
    
            [color=#007F00]' Set the eventhandler procedure, when the dummy1 variable is changed, this event fires.[/color]
            Class1_Set_DummyChanging( v, [color=#0000FF]CodePtr[/color]( Class1_DummyChanging ) )        
    
            [color=#007F00]' now set the dummy1 variable.., a msgbox with the old value should appear.[/color]
            Class1_SetDummy1( v, 123 )
    
            [color=#007F00]' Obtain the new value.[/color]
            [color=#0000FF]MsgBox[/color] "New value " & [color=#0000FF]Format$[/color]( Class1_GetDummy1( v ) )
    
        [color=#0000FF]End[/color] [color=#0000FF]If[/color]
    Here is the class module..
    Granted, while the macro's are certainly helpful, it's readability doesn't make it better. to bad..
    Note that all of this code is custom, how you use the VD_VarClass and Class1 functions is up to you.
    Code:
    [color=#007F00]'----------------------------------------------------------------------------------[/color]
    [color=#007F00]' The class's public data.[/color]
    [color=#007F00]' Just add/rename elements to the type variable.[/color]
    [color=#007F00]'----------------------------------------------------------------------------------[/color]
    [color=#0000FF]Type[/color] Class1_PublicData
    
        [color=#007F00]' Data[/color]
        Dummy1      [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
        Dummy2      [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
    
        [color=#007F00]' Events[/color]
        pcbOnClick  [color=#0000FF]As[/color] [color=#0000FF]Dword[/color]
    
    [color=#0000FF]End[/color] [color=#0000FF]Type[/color]
    
    [color=#007F00]'----------------------------------------------------------------------------------[/color]
    [color=#007F00]' Macro's[/color]
    [color=#007F00]'----------------------------------------------------------------------------------[/color]
    [color=#0000FF]Macro[/color] Class1_GetPublicDataByPtr
        [color=#0000FF]Local[/color] p [color=#0000FF]As[/color] Class1_PublicData [color=#0000FF]Ptr[/color]
        p = [color=#0000FF]VD_VarClass_GetPublicDataByPtr[/color]( pThis )
        [color=#0000FF]If[/color] p = 0 [color=#0000FF]Then[/color] [color=#0000FF]Exit[/color] [color=#0000FF]Function[/color]
    [color=#0000FF]End[/color] [color=#0000FF]Macro[/color]
    
    [color=#0000FF]Macro[/color] Class1_GetPublicData
        [color=#0000FF]Local[/color] p [color=#0000FF]As[/color] Class1_PublicData [color=#0000FF]Ptr[/color]
        p = [color=#0000FF]VD_VarClass_GetPublicData[/color]( vThis )
        [color=#0000FF]If[/color] p = 0 [color=#0000FF]Then[/color] [color=#0000FF]Exit[/color] [color=#0000FF]Function[/color]
    [color=#0000FF]End[/color] [color=#0000FF]Macro[/color]
    
    [color=#0000FF]Macro[/color] Class1_RaiseEvent_DummyChanging
        [color=#0000FF]MacroTemp[/color] p
        [color=#0000FF]Dim[/color] p [color=#0000FF]As[/color] Class1_PublicData [color=#0000FF]Ptr[/color]
        p = [color=#0000FF]VD_VarClass_GetPublicData[/color]( vThis )
        [color=#0000FF]If[/color] p > 0 [color=#0000FF]Then[/color]
            [color=#0000FF]If[/color] [color=#7F007F]@p.pcbOnClick[/color] > 0 [color=#0000FF]Then[/color] [color=#0000FF]Call[/color] [color=#0000FF]Dword[/color] [color=#7F007F]@p.pcbOnClick[/color] [color=#0000FF]Using[/color] Class1_OnClick( vThis )
        [color=#0000FF]End[/color] [color=#0000FF]If[/color]
    [color=#0000FF]End[/color] [color=#0000FF]Macro[/color]
    
    [color=#007F00]'----------------------------------------------------------------------------------[/color]
    [color=#007F00]' Eventhandlers, the event handler(s) should be prepared as the declare(s) below[/color]
    [color=#007F00]'-----------------------------------------------------------------------[/color]
    [color=#0000FF]Declare[/color] [color=#0000FF]Function[/color] Class1_OnClick( [color=#0000FF]ByRef[/color] vThis [color=#0000FF]As[/color] [color=#0000FF]Variant[/color] ) [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
    
    [color=#007F00]'----------------------------------------------------------------------------------[/color]
    [color=#007F00]' Wrapper to instantiate this class more easily and always the same.[/color]
    [color=#007F00]'----------------------------------------------------------------------------------[/color]
    [color=#0000FF]Function[/color] Class1_Create( [color=#0000FF]ByRef[/color] vNewObject [color=#0000FF]As[/color] [color=#0000FF]Variant[/color] ) [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
        VD_VarClass_CreateObject( Class1_PublicData, [color=#0000FF]CodePtr[/color]( Class1_Constructor ), [color=#0000FF]CodePtr[/color]( Class1_Destructor ), vNewObject )
        [color=#0000FF]Function[/color] = [color=#0000FF]IsTrue[/color]( [color=#0000FF]VD_VarClass_GetObjPtr[/color]( vNewObject ) )
    [color=#0000FF]End[/color] [color=#0000FF]Function[/color]
    
    [color=#007F00]'----------------------------------------------------------------------------------[/color]
    [color=#007F00]' The class's contructor and destructor, these are optional, you may remove the functions.[/color]
    [color=#007F00]'----------------------------------------------------------------------------------[/color]
    [color=#0000FF]Function[/color] Class1_Constructor( [color=#0000FF]ByVal[/color] pThis [color=#0000FF]As[/color] [color=#0000FF]Dword[/color] [color=#0000FF]Ptr[/color] ) [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
        Class1_GetPublicDataByPtr
        
        [color=#007F00]' Set default values.[/color]
        [color=#7F007F]@p.Dummy1[/color] = 1
        [color=#7F007F]@p.Dummy2[/color] = 2
    
    [color=#0000FF]End[/color] [color=#0000FF]Function[/color]
    
    [color=#0000FF]Function[/color] Class1_Destructor( [color=#0000FF]ByVal[/color] pThis [color=#0000FF]As[/color] [color=#0000FF]Dword[/color] [color=#0000FF]Ptr[/color] ) [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
    
        Class1_GetPublicDataByPtr
    
        [color=#007F00]' Free data.[/color]
        [color=#7F007F]@p.Dummy1[/color] = 0
        [color=#7F007F]@p.Dummy2[/color] = 0
    
    [color=#0000FF]End[/color] [color=#0000FF]Function[/color]
    
    [color=#007F00]'----------------------------------------------------------------------------------[/color]
    [color=#007F00]' Methods, better pass the variant object BYREF to avoid additional instantiating[/color]
    [color=#007F00]'----------------------------------------------------------------------------------[/color]
    
    [color=#007F00]' Set's the dummy variable.[/color]
    [color=#0000FF]Function[/color] Class1_SetDummy1( [color=#0000FF]ByRef[/color] vThis [color=#0000FF]As[/color] [color=#0000FF]Variant[/color], [color=#0000FF]ByVal[/color] Dummy1 [color=#0000FF]As[/color] [color=#0000FF]Long[/color] ) [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
        [color=#0000FF]Local[/color] hr [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
        Class1_GetPublicData
        Class1_RaiseEvent_DummyChanging [color=#0000FF]To[/color] hr
        [color=#7F007F]@p.Dummy1[/color] = Dummy1
        [color=#0000FF]Function[/color] = 1
    [color=#0000FF]End[/color] [color=#0000FF]Function[/color]
    
    [color=#007F00]' Obtains the value of Dummy1[/color]
    [color=#0000FF]Function[/color] Class1_GetDummy1( [color=#0000FF]ByRef[/color] vThis [color=#0000FF]As[/color] [color=#0000FF]Variant[/color] ) [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
        Class1_GetPublicData
        [color=#0000FF]Function[/color] = [color=#7F007F]@p.Dummy1[/color]
    [color=#0000FF]End[/color] [color=#0000FF]Function[/color]
    
    [color=#007F00]' Set's the eventhandler[/color]
    [color=#0000FF]Function[/color] Class1_Set_DummyChanging( [color=#0000FF]ByRef[/color] vThis [color=#0000FF]As[/color] [color=#0000FF]Variant[/color], [color=#0000FF]ByVal[/color] pcbOnClick [color=#0000FF]As[/color] [color=#0000FF]Dword[/color] ) [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
        Class1_GetPublicData
        [color=#7F007F]@p.pcbOnClick[/color] = pcbOnClick
    [color=#0000FF]End[/color] [color=#0000FF]Function[/color]
    Here is the eventcode used:
    Code:
    [color=#0000FF]Declare[/color] [color=#0000FF]Function[/color] Class1_GetDummy1( [color=#0000FF]ByRef[/color] vThis [color=#0000FF]As[/color] [color=#0000FF]Variant[/color] ) [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
    
    [color=#0000FF]Function[/color] Class1_DummyChanging( [color=#0000FF]ByRef[/color] vThis [color=#0000FF]As[/color] [color=#0000FF]Variant[/color] ) [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
    
        [color=#0000FF]MsgBox[/color] "Old value " & [color=#0000FF]Format$[/color]( Class1_GetDummy1( vThis ) )
    
    [color=#0000FF]End[/color] [color=#0000FF]Function[/color]
    You can find PwrDev here:

    http://www.hellobasic.com/pwrdev/

    Thanks,

    hellobasic
Working...
X