The Book of Visual Studio .NET - OOP in VB .NET Crash Course, Page 4
When you drag and drop controls onto a Windows Form, you are using objects. When you observe your code, you are looking at a class; when that code is loaded into memory, at runtime, it is considered an object. The importance of the distinction is simply to describe that a class is a template, while an object is an instance of that template in memory. Also, many copies of the template can exist in memory at the same time as objects.
Fortunately, we do not have to depend on the component designer to work with classes; we can build our own classes and components. This is nothing new for a moderately experienced developer; what is new is how Visual Basic .NET permits us to instantiate classes.
The two methods for instantiating classic COM are the CreateObject and New keywords. CreateObject uses the Windows Registry to obtain the interface of the class being instantiated. Because CreateObject depends on windows for access to the register, COM+ can apply a context for use by the COM+ services. The New keyword in classic COM also depends on Windows for access to the Windows Registry. The catch is that it doesn't always have to access the Windows Registry to discover a class's interface if the class resides inside the same component as the calling class. Because the New keyword has no problem accessing a class's interface within the same component, a class can be instantiated by passing COM+ services that would normally add a context or other component service. While this will not prevent you from loading a class into COM+, to take full advantage of COM+ services you should use the CreateObject keyword.
Having said all that, the CreateObject keyword cannot be used to instantiate .NET classes, although it can be used to instantiate classes that exist within classic COM components. Because .NET components don't rely on the Windows Registry, the New keyword is used when loading all .NET components. Here are several examples of how you might define and load classes into memory. First, the variable is diminished as MyClass:
Dim obj As MyClass
Second, you load the class "MyClass" into memory. An instance of a class loaded into memory is referred to as an object. Notice there is no "Set" keyword used.
Obj = New MyClass
Another method is to declare and instanciate an object in a single line:
Dim obj2 As MyClass = New MyClass()
Finally, you can implicitly diminish a variable with a class you are attempting to load. This is the shortest method and is perfectly acceptable:
Dim obj3 As New MyClass
Early and Late Binding
Binding is something we do when diminishing a variable, though many developers may not realize the importance of how they bind a class.
Early binding, often referred to as strong typing, refers to explicitly declaring the class used to define a variable. Early binding has several benefits. For example, when programming, Visual Studio .NET can give access to the class's interface with Intellisense which greatly reduces potential for typos and promotes rapid development. Also, when early binding a class, the Visual Basic compiler can enforce the proper use of a class's interface by providing warnings and refusing to complete the compile until the error is resolved. But performance gains are probably the most important reason to bind early: Early binding allows your program to access your class's interface directly, rather than through the Windows Registry or at runtime. If the compiler knows ahead of time which classes you will be using in your application, it can make the appropriate compilation optimizations.
Late binding can be useful when developing against non-existent components or ones that are being developed. Late binding allows you to continue compiling your code until the component is available; once the class is available, you can modify your code to early bind. You might also use late binding when you truly don't know the object type that will be passed to your function, in which case it is perfectly acceptable to accept any type of object. Before late binding can occur, the Option Strict option must be set to off. Option strict is off by default:
Option Strict Off
To declare a variable as lat e bound, simply diminish t he variable as type
object: Option Strict Off Dim obj As Object 'or Dim obj As System.Object
The System.Object class is the class from which all other classes are derived. While it has no specific characteristics that prevent it from acting as any other class, it is used for late binding.
Page 4 of 7