Section 3.5. Object Variables and Their Binding

   

3.5 Object Variables and Their Binding

In VB.NET, classes and their objects are everywhere. Of course, there are the classes and objects that we create in our own applications. There are also the classes in the .NET Framework Class Library. In addition, many applications take advantage of the objects that are exposed by other applications, such as ActiveX Data Objects (ADO), Microsoft Word, Excel, Access, various scripting applications, and more. The point is that for each object we want to manipulate, we will need to declare a variable of that class type. For instance, if we create a class named CPerson, then in order to instantiate a CPerson object, we must declare a variable:

 Dim APerson As CPerson 

Similarly, if we decide to use the ADO Recordset object, we will need to declare a variable of type ADO.Recordset:

 Dim rs As ADO.Recordset 

Even though object variables are declared in the same manner as nonobject variables, there are some significant differences. In particular, the declaration:

 Dim obj As MyClass 

does not create an object variable ” it only binds a variable name with a class name . To actually construct an object and set the variable to refer to that object, we need to call the constructor of the class. This function, discussed in detail in Chapter 4, is responsible for creating objects of the class.

Constructors are called using the New keyword, as in:

 Dim obj As MyClass = New MyClass(  ) 

or:

 Dim obj As MyClass obj = New MyClass(  ) 

VB.NET also provides a shortcut that does not mention the constructor explicitly:

 Dim obj As New MyClass(  ) 

(In earlier versions of VB, we use the Set statement, which is no longer supported.)

3.5.1 Late Binding Versus Early Binding

The object-variable declaration:

 Dim obj As Class1 

explicitly mentions the class from which the object will be created (in this case it is Class1). Because of this, VB can obtain and display information about the class members , as we can see in VB's Intellisense, shown in Figure 3-1.

Figure 3-1. Intellisense showing member list
figs/vnl2_0301.gif

As you know, Intellisense also shows the signature of a method, as shown in Figure 3-2.

Figure 3-2. Intellisense showing method signature
figs/vnl2_0302.gif

Of course, Intellisense is very helpful during program development. However, more important is that the previous object-variable declaration allows VB to bind the object's methods to actual function addresses at compile time . This is known as early binding .

An alternative to using a declaration that specifically mentions that class is a generic object-variable declaration that uses the As Object syntax:

 Dim   obj   As Object 

While it is true that obj can hold a reference to any object, we pay a major penalty for this privilege. VB can no longer get information about the class and its members because it does not know which class the object obj belongs to!

As a result, VB's Intellisense cannot help us with member syntax. More importantly, we pay a large performance penalty because VB cannot bind any of the classes, properties, or methods at compile time ” it must wait until runtime. This is referred to as late binding .

In summary, explicit object-variable declarations allow for early binding and thus are much more efficient than generic declarations, which use late binding. Hence, explicit object-variable declarations should be used whenever possible.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net