Visual Basic objects are usable examples of classes, which in turn are abstract representations of things. You first need to create classes before you can create objects. Fields, Properties, Methods, and Events make up the first half of the equation in object-oriented programming—paired with Encapsulation, Inheritance, and Polymorphism. Fields and properties contain the information about the object (such as color and name), while methods are actions you can perform with the object (such as stop and go), and events are notifications an object receives from—or sends to, other objects and applications (such as off and on). Encapsulation gives you the power to use classes with similar properties and methods. Inheritance gives you the ability to create a new class based on an existing class—without the need to recode all the fields, methods, properties and events. Polymorphism gives you control of all the classes interchangeably, so if you use one class for the color properties, you can use a similar class for the name properties (and vice versa). This article shows how to create Visual Basic Objects for a simple application that shows a bank account statement for a certain Mary Anne Doe.
Things You’ll Need
- Computer
- Visual Basic 5 or later
Creating Visual Basic Objects
- Start Visual Basic and create a new Windows Application named vbObjects. When the project opens, select the Project menu, and then choose Add Class.
- Name the new class file in the Add New Item dialog clsBankAccount.vb, and then click the Add button.
You will get a new tab in the code area for your new class file with the following declaration:
Public Class clsBankAccount
End Class
- Create class properties and extend your BankAccount class by adding member variables to hold the account name and number. Because you will be creating methods to access these values, mark the variables Private:
Public Class clsBankAccount
Private strAccountNumber As String
Private strAccountName As String
Private intBalance As Integer
Private accountFee As Integer = 5
End Class
- Define the class methods to access your data by using the Get and Set methods to access the properties in your code.
- Type in Public Property after Private accountFee As Integer = 5 to display the Get and Set method template, and fill in the information as follows:
Public Property AccountName() As String
Get
End Get
Set(ByVal value As String)
End Set
End Property
Public Property Balance() As Integer
Get
Return intBalance
End Get
Set(ByVal value As Integer)
intBalance = value
End Set
End Property
- Use the following object to call a method in your class and get the account balance for your client:
Dim objBankAccount As New clsBankAccount()
objBankAccount.AccountName = “Mary Anne Doe”
objBankAccount.Balance = 550
MessageBox.Show(objBankAccount.AccountName & ” has a balance of ” & CStr(objBankAccount.Balance))
The object code you created gets values for the account name and account balance properties of your object. Using the properties as reference, it displays the following message:
“Mary Anne Doe has balance of 550”.