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:
Similarly, if we decide to use the ADO Recordset object, we will need to declare a variable of type ADO.Recordset:
Even though object variables are declared in the same manner as nonobject variables, there are some significant differences. In particular, the declaration:
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, is responsible for creating objects of the class. Constructors are called using the New keyword, as in:
VB .NET also provides a shortcut that does not mention the constructor explicitly:
(In earlier versions of VB, we use the Set statement, which is no longer supported.)
Late Binding Versus Early Binding
The object-variable declaration:
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.
Figure : Intellisense showing member list
As you know, Intellisense also shows the signature of a method.
Figure : Intellisense showing method signature
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 largeperformance 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 Related Interview Questions
|VB.NET Interview Questions||ASP.NET Interview Questions|
|ADO.Net Interview Questions||Windows Presentation Foundation(WPF) Interview Questions|
|Windows CE .NET Interview Questions||Dot Net Framework Interview Questions|
|Asp Dot Net Mvc 4 Interview Questions||Asp Dot Net Mvc Interview Questions|
Variables And Data Types
Introduction To Object-oriented Programming
The .net Framework: General Concepts
The .net Framework Class Library
Delegates And Events
Error Handling In Vb .net
The Language Reference
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.