Object-based programming
Introduction
COM programming is based on an object-oriented style of programming. In VB, this means the use of classes and class-based object references. If you are comfortable defining your own classes with logical properties and methods, and then using these classes to instantiate And manipulate Objects, you can SAFELY SKIP THIS Section and Continue The Tutorial Here. Otherwise, Let's Begin!
We will study object-based programming in the context of an example:. A form-based progress indicator class called CProgress Here's a picture of this class in action (you should see this for yourself by running the application / VBCOM / Demos / Class- Based Progress Indicator / App.exe; if you have not already DONE SO, You Can Download The Labs from The Setup Page:
Note the naming convention --- classes always being with a C. It will be helpful if you also think of the C as standing for a concrete class, i.e. a class that can be turned into a run-time object that fulfills some function.
First, a bit of terminology. A class is compile-time entity that a programmers writes. In VB, a class is produced by adding a Class Module to your project. A class may contain zero or more members, where a member is either a property (think of it as a variable) or a method (subroutine or function). At run-time, you instantiate a class to produce an object that resides in memory. in fact, you can instantiate the same class over and over again, each time producing a distinct object Finally, to facilitate discussion, we will use the terms client and server to denote two objects that are interacting, in particular where the client object is accessing a property or invoking a method on the server object.:
USING THE CPROGRESS
THE CLASS IS QUITE SIMPLE IN Design, Having Only One Public Property and TWo Public Methods:
Public value as integer '** percentage done public SHOW ()' ** show the program for public sub hide () '** Hide The Progress Form
When this class is instantiated into an object, the Value of the object represents the current status of the progress indicator, eg 40 (denoting 40%). By changing this value, the client can show progress. The object's methods, Show and Hide, .
At this point, let's look at some sample client code which instantiates and uses the CProgress class. If you have not already, open the supplied VB project / VBCOM / Demos / Class-based Progress Indicator / App.vbp and view the code in The cmdprogress button of the main form:
DIM Progress as cprogress' ** Object Reference to '** CProgress Instance Dim I as Integer, _ J AS Variant Set Progress = _ new cprogress' ** New Instance of CPRESS
The first line declares an object reference variable (a pointer in C parlance) of type CProgress. Initially, an object reference variable points to no object, and thus has the value Nothing. The third line uses VB's New operator to instantiate an object of class CPROGRESS, AND THEN STS The Object Reference Variable Progress to this new instance. At this point we have:
The Code Continues by Displaying The Progress INDICATOR FORM, AND THEN EXECUTING A LOOP Which Simulates a long-running operation:
Progress.show '** invoke show method for i = 1 to 10 progress.value = _ progress.value 10' ** update by 10% for j = 1 to 1000000: Next J '** Pause to Simulate Activity Next IEACH Time through the loop, progress increas by 10%. Finally, overce the loop completion from the screen and the program:
Progress.hide '** Invoke Hide Method Set Progress = NOTHING' ** EXPLICITLY DESTROY Instance
This last step is not strictly necessary, since VB will reset the variable to Nothing automatically once it goes out of scope. However, it is important to note that the number of references to an object determines the lifetime of that object, ie the duration it exists in memory. by setting progress to Nothing we are explicitly reducing the reference count of that object by 1. Once an object's count reaches 0, it is destroyed. Thus, in the example above, the object is immediately destroyed after the Set statement is Executed.
Implementing the class cprogress
If you have not done so already, open the supplied VB project / VBCOM / Demos / Class-based Progress Indicator / App.vbp and view the code in the Class Module CProgress (stored in the file CProgress.cls). Using VB's browser (F2), The Class is Summarized as Follows:
The class actually contains two properties and four methods, although the first 3 listed above are private to the class (and thus accessible only to VB and the object itself). The private property frm is used by the object to hold a reference to the Form instance containing the progress indicator. This form is created in the method Class_Initialize, which is called automatically by VB whenever a new CProgress object is instantiated. Likewise, the form is destroyed in the method Class_Terminate, called automatically when the object's reference count reaches 0. Here's the relevant code from the class CProgress: Option Explicit Private frm As form '** object reference to progress form Private Sub Class_Initialize () Set frm = New frmProgress' ** create a new instance of frmProgres Me.Value = 0' ** Initialize Our Value to 0% End Sub Private Sub Class_Terminate () Unload Form '** Destroy Progress Form Instance Set FRM = Nothing End Sub
In this case, the method class_initialize is caled behind the scene in response to the client executing new:
Set progress = new cprogress '** create a new' ** instance of cprogress
Thus, when developing a class, use the Class_Initialize method to initialize the server for upcoming client use As for Class_Terminate, in this case the method is called when the client clears the last reference to the server.:
Set progress = Nothing '** Explicitly Destroy Instance
Therefore, use the class_terminate method to cleanup (Close Files, Destroy Helper Objects, etc.) Before.
Now for the public interface of CProgress:. Value, Show and Hide Firstly, the Value property is not implemented using an integer variable, but instead is a logical property physically realized by appropriate Get and Let methods (look closely at the bottom pane of the browser window above) Logical properties offer many benefits:. validation, the ability to take action when a property changes, read-only data, etc. However, a stronger motivation is that COM simply does not allow a class to contain public data properties. in a COM-compatible class, the only public members may be methods.Ignoring COM for the moment, a logical value property makes sense in this situation since the server needs to update the progress indicator in response to a change in value. The Let method IS Used to Capture A Write to The Logical Property, Much Like The Let Statement in VBA Assigns A New Value To a variable. Note That The New Value IS Passed As a parameter to the method:
Public Property Let Value (ByVal percentage As Integer) frm.pbarProgress.Value = percentage '** update progress bar frm.lblPercentage.Caption = _ CStr (percentage) & "%"' ** update label frm.Refresh '** redraw Form End Property
The new value is simply assigned to the underlying Windows 95 ProgressBar control on the progress form, as well as written to a label In order for the client to be able to read the current progress value, a corresponding Get method is also provided.:
Public property get value () AS integer '** Return Current State of Progress Bar value = frm.pbarprogress.value End Property
Get Methods Are Actually Functions Which Return The Current Value of The Logical Property.
The final two members of CProgress are the Show and Hide methods These are perhaps the easiest of the bunch, since their function is obvious:. Public Sub Show () frm.Show '** show form and redraw frm.Refresh End Sub Public Sub Hide () frm.hide '** Erase Form But Leave Form Object in Memory End Sub
That's It, The Class CProgress is now Completely Implement!
Classes vs. instances
Before we begin Our Discussion of COM, Let's Review The notion of classes, and instances of classes. Consider The Following Client Code:
DIM P1 As CPROGRESS, P2 As CProgress, P3 As CPROGRESS
Set p1 = new cprogress set p2 = new cprogress set p3 = new cprogress p1.value = 40 p2.Value = 5 p3.Value = 99 p1.show p2.show p2.hide stop '** pause client and activate the debugger
How many instances of CProgress exist in memory? Three! How many progress indicator forms exist in memory (ie instances of frmProgress)? Three! How many of these form instances are visible on the screen? One! Okay, now you're ready to Start The First Lesson ON.