Classes in VB

Terminology

Classes are like cookie cutters;  you don't eat the cookie cutter, but you use it to make cookies.

The cookies are the objects. When you make a cookie using a cookie cutter, you instantiate an object of the cookie class.

Create a New Class

  1. Open a new project
  2. Select Add Class Module from Project menu
  3. From the New tab, choose Class Module and click OPEN
  4. Change the Name property in Properties to CProduct

Define the Class Properties (the members in the class)

  1. In General Declarations, define the variables as Private

' Use the prefix "m" (for module) and the data type for each property
Private mcurPrice As Currency
Private mintQuantity As Integer
Private mstrDescription As String

Add Property Procedures (the let and get members of the class)

  1. From the Class Module code window, select Add Procedure  from Tools
  2. name= Quantity which is public and a procedure
  3. Change the return type to integer in the get procedure

    Public Property Get Quantity() As Integer
    ' used to return Quantity to the caller
    Quantity = mintQuantity
    End Property
     

  4. Change the parameter name to intQuantity and type to integer in the let procedure

    Public Property Let Quantity(ByVal intQuantity As Integer)
    ' used to set Quantity with a caller-supplied parameter
    mintQuantity = intQuantity
    End Property

     

  5. Add Procedures for Price (currency) and for Description (string)

Public Property Get Price() As Currency
Price = mcurPrice
End Property

Public Property Let Price(ByVal curPrice As Currency)
mcurPrice = curPrice
End Property


Public Property Get Description() As String
Description = mstrDescription
End Property

Public Property Let Description(ByVal strDescription As String)
mstrDescription = strDescription
End Property

Code a Method

  1. Add Procedure ExtendedPrice which is a function and is public
  2. Make the return type currency

    Public Function ExtendedPrice() As Currency
    ' Calculate the value of the product
    ExtendedPrice = mcurPrice * mintQuantity
    End Function

Creating a New Object Using a Class

  1. Open the form, and create your GUI:


     

  2. Define the object in General Declaration

    Private mproduct As CProduct
     

  3. Create a new sub procedure that will assign values from the form to the properties of the new object

    Private Sub AssignProperties()
    ' Assign the properties from the form
    ' if not instantiated in general declaration

    ' use New to allocate the object - just like C++

    Set mproduct = New CProduct 'instantiated at first use

    mproduct.Description = txtDescription.Text
    mproduct.Price = Val(txtPrice.Text)
    mproduct.Quantity = Val(txtQuantity.Text)

    End Sub

     
  4. Code the command button procedures (Calculate and Exit)

    Private Sub btnCalculate_Click()
    ' Calculate the inventory value
    ' But first, assigning all the product property on the form to the object
    AssignProperties
    ' calculate the inventory value using the object's method
    lblInventoryValue = mproduct.ExtendedPrice

    End Sub

    Private Sub btnExit_Click()
    End
    End Sub

Run It !

Download:  the form, the class, the project

Next: Collections


Last update:  Wednesday February 18, 2009
Questions:
 email Prof Domanski