VB Class Modules
There are three basic tasks in creating an Object Linking and Embedding (OLE) Automation Server: class creation, OLE Automation, and remote automation. I will deal with each of these topics separately. This month in Part 1 of this series, I'll show you how to create and use VB classes and objects. Part 2, OLE Automation, will show you how to create object collections, handle errors, and build object models. And Part 3, Remote Automation, will discuss how to distribute these objects across the network, where they're actually running on remote machines.
Class modules, new in Visual Basic 4.0 (VB4), allow you to create encapsulated, reusable objects. When incorporated into an OLE Automation server, these objects can be used by external applications. If you use the remote-automation capability of the Enterprise Edition of VB4, they can even be located on remote machines.
VB4 classes encapsulate both code and data--in object-oriented terms--as methods and properties, respectively. These concepts are already familiar to those who have used VB's forms and controls in the past. VB has always supported properties and methods, as well as events: In fact, the language has built-in events, methods, and properties. You can create new properties and methods, but not events, for the classes you define. In fact, VB forms and controls have always been defined by classes, and handling them now as objects makes even more sense. For instance, the following code for creating a form at runtime has always worked: Dim frm as Form, Set frm = New Form1.
Many classes are virtually unchanged from previous versions of VB: If you use the Object Browser to look at built-in VB objects, you will find some that are familiar, including App, Form, and Screen, as well as individual classes for each of the built-in controls. These are objects that you can create in VB4 and expose through OLE Automation via a type library, which contains information on each class's properties and methods, including suggested syntax and links into an associated Help file.
There is also a Control class, which is often referred to as a generic object type. Using it, you can create an object variable that works with any control type: This is most useful when passing several types of controls to a subroutine, but it's also handy when setting or examining properties for all controls on a form by looping through the Controls collection. However, programming with specific object types provides better performance because less object-binding information has to be determined at runtime.
Some supplied classes have changed a bit with VB4. For instance, the data-access objects for working with the Jet database engine (e.g., Table, DynaSet, and SnapShot) have been superseded by the RecordSet objects. Because VB4 allows you to add references to object libraries via the Tools | References command--first seen in the VB for Applications (VBA) included with Excel 5.0 and Project 4.0--using external objects is now more straightforward.
This VB3 code was necessary to obtain a reference to an Excel worksheet:
Dim objXL As Object
Dim objWS As Object
Set objXL = CreateObject("Excel.
Application")objXL.Workbooks.Add Set objWS = objXL.ActiveSheet
In VB4, it can be replaced with something as simple as:
Excel.Workbooks.Add
Set ws = Excel.ActiveSheet
There are even a couple of standard OLE objects that you can use: the built-in StdFont and StdPicture. To create a stand-alone font object that can be assigned to other objects or passed among applications that understand it, you might use code like this:
Set fnt = New StdFont
fnt.Size = 24
fnt.Bold = False
fnt.Italic = True
This font object can be assigned to a VB form or control: Form1.Font = fnt. In a similar way, you can assign a VB form or control's font object to a font variable:fnt2=Form1.Font.
VB Objects
Now that you know how to use existing objects, let's look at how to create new ones in VB. Class modules are dead simple to create: You insert a class module into your project just as you would a VB form or code module. Classes have very little overhead, needing only 40 bytes of runtime memory space for the class definition. (It's a good thing, too, because each object in your application requires its own class module.) The objects created from this definition are slightly more expensive: Each object created at runtime--instantiated, in object-oriented terminologyrequires approximately 72 bytes of memory.
If you start with a VB class module, the simplest way to add properties is to declare a Public variable in the class's General Declarations section:PublicStuff asString. Similar to the Global keyword in previous versions of VB, the Publickeyword indicates that other classes-- or modules--can see it. When you start your minimal application and execute DimfooAsNewMyObject--or, alternatively, DimfooasMyObject, Setfoo= NewMyObject--you have created an instance of MyObject with the single property,Stuff.This property can be as-
signed as foo.Stuff = "Comment Area", and its value can be retrieved by varTemp = foo.Stuff.
Reading and writing a property this way, however, leads to uncontrollable results: Unfortunately, there is no way to check for runtime access, security, and data validation. Runtime access is significant because many object properties, including Name, may not be changeable at runtime. Without a doubt, security is important when you have multiple users of an object and you don't want all of them to have access to a property. For instance, perhaps only managers should have access to a Salary property in an Employee object. Data validation is obvious when you consider such commonly implemented properties as Visible and Enabled: Setting these to anything other than True or False could lead to a whole host of errors in your program.
To address all of these situations, access to properties can be contained in property procedures. For each property, you can use a Property Get procedure to retrieve its value and a Property Let procedure to assign it a value. For instance, instead of the Public declaration for Stuff used above, a private declaration, Private sStuff As String, can be used along with the following Property Let procedure:
Public Property Let Stuff(sTemp)
'Data validation code
'...
sStuff = sTemp
End Property
The corresponding Property Get procedure is:
Public Property Get Stuff(sTemp)
sTemp = sStuff
End Property