To start we create a "Class Module" and rename it car. For this example we are going to make a basic object that represents the data a car garage may hold on a car.
Next we create Variables that are going to store different bits of information of the car class.
Properties are also assigned, In this example we make the Owner accessible.
Events are created in this example we assign default values to each of the properties when a class instance is created.
Finally methods are created which allows for standard ways we can interact with the data stored in the class.
In the follow example we show how we can use the class we have created. We also use the method created to assign a new owner to the car.
Class Code:
'Variables
Public Name As String
Private Owner As String
Private Value As Double
' Properties
Property Get CarOwner() As String
CarOwner = Owner
End Property
' Event
Private Sub Class_Initialize()
Value = 100
Owner = "Garage"
End Sub
' Methods
Public Sub transfer(NewOwner As String)
Owner = NewOwner
End Sub
Public Sub Discount(Amount As Double)
Value = Value - Amount
End Sub
Example Application Code:
Sub classtest()
Dim oCar As New Car
'Setr Car name
oCar.Name = "Ford"
MsgBox (oCar.Name)
MsgBox (oCar.CarOwner)
oCar.transfer ("Brian")
MsgBox (oCar.CarOwner)
End Sub
Related Sections
Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.Must Read Articles
VBA and Microsoft Excel - Getting the most out of excel with VBA programming
VBA and Microsoft Word - Getting the most out of word with VBA programming
Application Object - Using the applications features in your code.
Installing VBA and First Program - What do you need to start using visual basic application VBA.