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.
- Makes your code well-organized
- Makes code easier to read and maintain
- Better organized code is easier to test and therefore more reliable
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
|Alt-CCI | Menu: Code VB » Class and collection » Insert Class|
- Start Insert Class from the Code VB menu
This opens the dialog asking you to give the class a name
- 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 to see how to create and use a custom class.
|Alt-CCP | Menu: Code VB » Class and collection » Add Property|
|Alt-CDP | Menu: Code VB » Declare » Property|
- Start Add Property from the Code VB menu
This opens the dialog asking you to give the property a name
- Type the name for the property - as an example we intered property 'Name'
- Decide if you want to make the property available for read (Get) write (Let/Set) or both and set the checkbox correspondingly
- Optionally select the Public or Private radio buttons
- 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
|Alt-CDF. | Menu: Code VB » Declare » Function » <type>|
|Alt-CDS | Menu: Code VB » Declare » Sub|
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.
- Declare class variable
- Initialise object
- Use With - optional syntax for efficiently calling a set of class procedures
- Set properties and call a method
Dim prsPerson As Person Set prsPerson = New Person With prsPerson .Name = "John Doe" .Age = 37 End With prsPerson.Celebrate