Using Code VB to create and use classes

Introduction - why use classes

Before we jump into the deep, what benefits will you get from using classes? Why the trouble? One of the nice features of procedural programming (not using classes) is that it is so straight-forward. To achieve task T, first do this, next that and finished. Unfortunately, in many cases the task is not so easy, and while programming more and more requirements get added. Now it is possible to continue adding pieces of code here and there, but after a while your program code is full of patches. Code created this way reflects the order in which requirements were added instead of the logical structure of the problem.

How to decide on classes

Working with the Office objects, be it Excel, Word, Access or Outlook should give us a good basic understanding of what classes are. As an example, in Word we have Application, Documents, Header, Printer - and hundreds more. This brings us to the main characteristic of well-chosen classes: the intuitive meaning and scope of class should be undisputed.

If you worked with databases, you will see a correspondence with 'data model' with tables, fields and relationships. Here we have a similar thing, called 'object model', with properties and methods.

Further reading on classes in VB see MSDN: Custom Classes and Objects.

How to create a class with properties and methods

As an example you will see the creation of a simple sample class AddressBlock whose use it is to make sure an address gets fully displayed in the address area of an envelope

Insert Class

menu vba class
  1. Start Insert Class from the Code VB menu
    This opens the dialog asking you to give the class a name
  2. Type the name for the class and press Enter

The new class is created and added to your project. Now you can start adding properties and methods.

Click this Start demo screencast to see how to create and use a custom to create and use class

Add Properties to a class

  1. Start Add Property from the Code VB menu
    This opens the dialog asking you to give the property a name
  2. Type the name for the property - as an example we intered property 'Name'
  3. Decide if you want to make the property available for read (Get) write (Let/Set) or both and set the checkbox correspondingly
  4. Optionally select the Public or Private radio buttons
  5. Press Enter to have the declaration code for the property inserted
Private mstrName As String

Public Property Get Name() As String
    Name = mstrName
End Property
Public Property Let Name(rData As String)
    mstrName = rData
End Property
  • By default in visual basic properties are public. You can limit accessibility by replacing it by 'Private'

Add Methods

Methods are either Sub or Function and are inserted the same way as in standard modules.

How to use a custom class in code

The code below shows sample code for working a custom class.

    Dim prsPerson As Person
    Set prsPerson = New Person
    With prsPerson
        .Name = "John Doe"
        .Age = 37
    End With
  • Use the Class_Initialize event to do any work with the object at creation time
  • Class initialisation does not allow use of arguments - you will have to set properties instead