Code:
'______________________________________________________________________________ ' ' Inheritance: Shared Data Demo ' ----------------------------- ' ' Demo showing how to share data between the base class and derived interfaces. ' ' ' By Kev Peel, August 2008. [URL]http://www.kgpsoftware.com[/URL] '______________________________________________________________________________ #Compile Exe #Dim All #Include "win32api.inc" ' For the DebugPrint function. '------------------------------------------------------------------------------ ' For debugging purposes. Creates and displays text in a console. '------------------------------------------------------------------------------ Function DebugPrint(ByVal sText As String) As Long Static hCons As Dword hCons = GetStdHandle(%STD_ERROR_HANDLE) If (hCons = 0) Or (hCons = %INVALID_HANDLE_VALUE) Then AllocConsole hCons = GetStdHandle(%STD_ERROR_HANDLE) If (hCons = %INVALID_HANDLE_VALUE) Then Exit Function End If ' Write to the screen... Local lCount As Long sText = sText + $CrLf If WriteConsole(hCons, ByVal StrPtr(sText), Len(sText), lCount, %NULL) Then Function = %TRUE End Function '------------------------------------------------------------------------------ ' The base class. One simple interface. '------------------------------------------------------------------------------ Class CBase Instance bInit As Byte Instance saved_data As Long Class Method Create ' Initialize... DebugPrint "CBase.Create" End Method Class Method Destroy ' Uninitialize... DebugPrint "CBase.Destroy" End Method Interface IBase Inherit IDispatch '------------------------------------------------------------------------------ ' Internal: Saves data in nValue (could be a pointer), for future base access. '------------------------------------------------------------------------------ Method PassData(ByVal nValue As Long) If (bInit = %FALSE) Then ' Save data ONCE only, since this is public... bInit = %TRUE saved_data = nValue DebugPrint "IBase.PassData(" + Format$(saved_data) + ")" End If End Method ' Put your base METHOD/PROPERTY code here '------------------------------------------------------------------------------ ' Displays the saved data that was passed from PassData. '------------------------------------------------------------------------------ Method ShowData DebugPrint "IBase.ShowData: saved_data is " + Format$(saved_data) End Method End Interface End Class '------------------------------------------------------------------------------ ' Simple class #1, uses an interface derived from IBase. '------------------------------------------------------------------------------ Class CDerive1 Class Method Create ' Initialize... DebugPrint "CDerive1.Create" ' Access our secret METHOD... ' Note: it passes a number here, but you can pass anything, even a pointer ;) Local oSecret As IDerive1 Let oSecret = Me oSecret.PassData(111) End Method Class Method Destroy ' Uninitialize... DebugPrint "CDerive1.Destroy" End Method Interface IDerive1 Inherit CBase, IBase ' Put your derived METHOD/PROPERTY code here End Interface End Class '------------------------------------------------------------------------------ ' Simple class #2, uses an interface derived from IBase. '------------------------------------------------------------------------------ Class CDerive2 Class Method Create ' Initialize... DebugPrint "CDerive2.Create" ' Access our secret METHOD... ' Note: it passes a number here, but you can pass anything, even a pointer ;) Local oSecret As IDerive2 Let oSecret = Me oSecret.PassData(222) End Method Class Method Destroy ' Uninitialize... DebugPrint "CDerive2.Destroy" End Method Interface IDerive2 Inherit CBase, IBase ' Put your derived METHOD/PROPERTY code here End Interface End Class '------------------------------------------------------------------------------ ' The test code, creates some objects. '------------------------------------------------------------------------------ Function PBMain Local Obj1 As IDerive1 Local Obj2 As IDerive2 Obj1 = Class "CDerive1" Obj2 = Class "CDerive2" Obj1.ShowData Obj2.ShowData Obj1 = Nothing Obj2 = Nothing ? "END" End Function