Changes

Jump to: navigation, search

Object Oriented Programming with Visual Basic

6 bytes removed, 19:52, 9 April 2009
no edit summary
Before an object can be instantiated we first need to define the class 'blueprint' for the object. In this chapter we will create a Bank Account class to demonstrate the concepts of Visual Basic object oriented programming.
Being Begin by starting Visual Studio and creating a new Windows Application named ''vbObjects''. Once the project opens, select the ''Project...'' menu and select ''Add class...''. In the ''Add New Item'' dialog, name the new class file ''clsBankAccount.vb'' and click on the ''Add'' button.
Visual Studio will now add a new tab to code area for the new class file containing the following class declaration:
</pre>
We have now defined a class. The next step is to add some functionality to the class.
== Creating Visual Basic Class Properties ==
Class members or properties are essentially variables embedded into the class. Members can be ''Public'' or ''Private''.
''Public'' members can be accessed from outside the object. ''Private'' members can only be accessed by methods contained in the class. This is the key to what is called ''data encapsulation''. Object-oriented programming convention dictates that data should be encapsulated in the class and accessed and set only through the methods of the class (typically called ''getters'' and ''setters'').
We can now extend our BankAccount class to add member variables to hold the account name and number. True to the concept of data encapsulation we will be creating methods to access these values later, so will mark them as ''Private'':
== Defining Class Methods ==
<google>ADSDAQBOX_FLOW</google>
Since we have declared our our class data properties to be private, we need to provide methods which will give us access to those properties from our code. This is achieved using Get and Set methods. The syntax for get and Set methods is as follows:
'''Public Property''' ''propertyName()'' '''As''' ''datatype''<br>
</pre>
Now that we have defined our getter and setter methods we can add our own method to perform a task. This is the same as writing a Visual Basic function. The purpose of our method will be to subtract the account fee from the current balance:
<pre>
</pre>
When called, the above function subtracts the account fee from the balance, assigns thje the new balance to the intBalance variable and returns the new total.
Now that we have added some functionality to our class it is time to instantiate objects from the this class ''blueprint''.
== Instantiating an Object from a Visual Basic Class ==
== Accessing Object Properties and Methods ==
Now that we know how to write a class and instantiate objects from the class, we now need to know how to call the methods on the methods we created in the class. First, you will recall that we created Get and Set functions the data members of our class. Because we did this, we can change and access these properties simply by reference referencing them as we would any other property. For example:
<pre>
</pre>
The above code excerpt assigns values to the AccountName and account balance properties of our object. It then references the properties in order to display a string with which reads "John Smith has balance of 1230" in a MessageBox.
We can also call our subtractFee() method as follows:

Navigation menu