Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


February 1996

OLE Automation Server


RSS
Subscribe to Windows IT Pro | See More OLE DB Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
SideBar    Glossary (OLE Automation)

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 terminology­requires 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

   Previous  [1]  2  Next 


Top Viewed ArticlesView all articles
CES 2009: Ballmer Announces Windows 7, Windows Live, Live Search Milestones

During his first-ever Consumer Electronics Show (CES) 2009 keynote address last night in Las Vegas, Microsoft CEO Steve Ballmer announced the pending public availability of a feature-complete Windows 7, the final version of Windows Live Essentials, and ...

10 Reasons Not to Deploy Windows Vista

The decision to upgrade to Vista has to make business sense, but many companies find the costs in training and application compatibility problems outweigh any benefits Vista brings. ...

10 Reasons to Deploy Windows Vista

The decision to upgrade your XP systems to Vista is simple when you consider features such as easier backup, a great desktop search, and vastly improved security options. ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Cloud Computing Forum: Integrating Software, Server and Storage as a Service into Your Enterprise IT Delivery Model

Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing