Date: 02-16-2022
Return to Index
created by gbSnippets
CLASS is like and enhanced UDT
CLASS is used to define an Object
An Object is an INSTANCE of a CLASS
Just like an application, an object contains variables and
methods (methods can be made to work like a SUB or a FUNCTION).
Some variables/methods can only be accessed within the object.
Those variables/methods which can be accessed outside the
object are called the Interface to the object
To declare a variable as an object variable, requires 2 steps.
First, create a variable where an Interface is used instead of a data type.
Local X As MyObjectInterface
Second, assign an object reference to the variable, using the CLASS name.
Let X = MyObject
Here are both steps:
Local X As MyObjectInterface
Let X = MyObject
At that point if MyObject has a variable var or method mthd that is part
of the interface (externally accessible), you could access them like this:
w = X.var
w = X.mthd 'there could also be arguments to the method, like a Sub/Function
A DLL containing objects is called a COM object (also known as
an OCX or ActiveX Object) and is an in-process object.
Objects may also be placed in an EXE, available to be called by
outside applications. The EXE is called a controller and is said
to contain out-of-process objects.
Each CLASS can have a unique 128-bit string (GUID) It is also
called a ClassID, or CLSID. A unique GUID is assigned to each:
Class CLSID Friendly version is called PROGID
Interface IID
COM application APPID
Library LIBID
#COM GUID GUID$("{20000000-2000-2000-2000000000000002}")
$MyLibGuid = GUID$("{00000099-0000-0000-0000-000000000007}")
$MyClassGuid = GUID$("{00000099-0000-0000-0000-000000000008}")
$MyIfaceGuid = GUID$("{00000099-0000-0000-0000-000000000009}")
PowerBASIC GUID$ statements generates random GUID
GUIDs are typically ignored when you create objects
just for internal use within your program (PowerBASIC
randomly assigns GUIDs if you do not).
Classes have Variables and Methods. Some
may be only accessed internally. Some may
be accessed externally.
Methods are simply object functions.
CREATE and DESTROY are optional methods, but
if present must be those names. CREATE is
automatically run when object is created.
DESTROY is run when object is destroyed.
Virtual Function Table provides pointers to
each interface Method or Property. PowerBASIC
handles this automatically.
Two types of Interface
iUnKnown - basis for every object
iAutomation - Direct - faster, uses VFT lookup for speed
iDispatch - Dispatch - slower, legacy interface
To publish an Object, the Object and Interface
must be given a Class ID GUID.
$MyClassGUID = GUID$(...)
$MyInterfaceGUID = GUID$(...)
Class MyClass $MyClassGuid As COM
Interface MyInterface $MyInterfaceGUID
'Define an Object:
Let MyObject = OBJECT "MyClass" 'internal object
Let MyObject = NEWCOM ...
'Define a Class:
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
Function PBMain() As Long
Local Z As MyInterface 'z is object variable with interface MyInterface
Let Z = Class "MyClass" 'z is object of class "myclass". Interface is implied by object variable
? Str$(Z.x) 'uses Get
Z.MM 'uses exposed method MM
? Str$(Z.x) 'uses Get
Z.x = 14 'uses Set
? Str$(Z.x) 'uses Get
End Function
Class MyClass
'hidden stuff -----------------------------------------
Instance X As Long 'class variable
Class Method Create() : ? "Object created" : End Method 'startup method (optioanl)
Class Method Destroy() : ? "Object destroyed" : End Method 'end method (optional)
'exposed stuff -----------------------------------------
Interface MyInterface 'exported methods/variables
Inherit IUnknown 'inherit the base class
Property Get X As Long : Property = X : End Property 'Property Get parameters are optional
Property Set X (V As Long) : X = V : End Property 'Property Set requires at least 1 parameter
Method MM() : Incr X : End Method 'Exposed method
End Interface
End Class
'gbs_01341
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm