Changes

Jump to: navigation, search

C Sharp Object Oriented Programming

1,827 bytes removed, 16:39, 18 January 2008
Instantiating an Object from a Visual Basic Class
Now that we have added some functionality to our class it is time to instantiate objects from the class ''blueprint''.
== Instantiating an Object from a Visual Basic Class ==
The process of creating an object from the class 'blueprint' is called instantiation. Essentially, you instantiate an instance of the class and give that instance a name by which you will refer to it when accessing members and calling methods. You can create as many object instances of a class as you desire. Objects are instantiated from a class using the ''new'' keyword. Visual Basic provides two ways to instantiate an object from a class. These two alternatives are known as late-binding and early binding.
 
Late binding occurs when the assignment of the object to the variable occurs at run-time when execution reaches the code where the object is created. There are drawbacks to this approach in that any syntax checking is not performed until the application runs because the compiler has no way of checking whether you are referencing valid class members, resulting in potential run-time exceptions. Another disadvantage is that execution can be slowed when using late-binding since much of the work performed by the compiler when using early binding has to be performed at runtime.
 
Late binding is achieved using the ''new'' keyword as follows:
 
<pre>
Dim objBankAccount As Object
 
objBankAccount = New clsBankAccount()
</pre>
 
Early binding, on the other hand, has none of the draw backs of late-binding. The compiler performs syntax checks to ensure the object is being used correctly and execution speed is increased because the binding takes place at compile time - not at run-time.
 
Early binding is performed as follows:
 
<pre>
Dim objBankAccount As clsBankAccount
 
objBankAccount = New clsBankAccount
</pre>
 
It is also possible to instantiate the object at the same time that it is declared:
 
<pre>
Dim objBankAccount As New clsBankAccount()
</pre>
== Accessing Object Properties and Methods ==

Navigation menu