5.1. Creating Object Model References

Before you can use the properties, methods, and events of an object model, you must first create a programmatic reference to the class containing the properties, methods, or events you wish to use. You do this by declaring a local object variable to hold a reference to the object. You then assign a reference to the object to the local variable. There are two methods, detailed next, for doing this in VB.

5.1.1. Generic Object References and Late Binding

In situations in which the object you are instantiating doesn't provide a type library, or when you are unsure at design time precisely which object you need to reference, you can use a local variable dimensioned as the generic type of Object. Then you use the CreateObject or GetObject function to return an object reference to assign to the local generic object variable. For example:

Dim oObjVar As Object
Set oObjVar = CreateObject("myComponent.myClass")

VB6 has also added to the functionality of the CreateObject function by allowing you to pass the server machine name as a string parameter. (For details on how to use CreateObject and GetObject see Chapter 7.)

Sidebar 1. If You Haven't Set It, You Can't Use It!

To assign an object reference to a local object variable, you must use the Set statement. The sole exception is the For Each...Next loop, which can iterate the object members of a collection. It generates an object reference automatically for each object that it iterates.


This method of creating an object reference produces a late bound interface; your application has no way of knowing in advance (that is, at design time) what the makeup of the object interface is. Only at runtime does your application get to bind to the interface; hence the term late binding. Because of this, you won't be given any help from VB's IntelliSense statement completion feature when writing your code; see Section 5.2.

5.1.2. Early Binding

The other—and now most common—method of adding an object reference to your project is by using the References dialog, which is shown in Figure 5.1. You can access the References Dialog by selecting the References option from the Project Menu. All OLE Automation Servers registered on your machine are shown in the list. To create a reference to one of them, check the box and click OK.

Figure 5.1. The References dialog

Once you have added a reference to your project in this manner, you can create an early bound interface to the object by using the Private, Public, Friend , or Dim statement to declare an object variable of the exact type. Then use the Set statement to assign an object reference to the local variable. (For details on how to use the Private, Public, Friend , Dim , and Set statements, see their entries in Chapter 7.) For example:

Dim objDoc as Document
Set objDoc = Word.Application.ActiveDocument

You can also use the New keyword with the Private, Public, Friend, or Dim statement, as in the following example:

Dim objDoc As New Document

Because your project has a reference to the object's type library, binding to the object can take place at compile time, which makes your application more efficient and allows you to get help in your coding from IntelliSense.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.107.116