Classes in Visual Basic


Encapsulation

Encapsulation is the protection of the private class data that is accessible ONLY through the public accessor functions. 

It has been noted that the interface of the class is what the client applications see. The protection of private class variables is an important feature of object oriented programming. This means that the internal representation of the data is protected and access to the data is only available by using the public interface. The client really has no knowledge of the internal representation of the data that drives the operation of the class.


Polymorphism

Polymorphism means that similar objects behave in a similar, although not identical way. In VB, this feature is implemented using naming conventions. Perhaps a good example of how this works is with the Print method of the Form and Debug object. These are similar objects and the Print method for each does the same thing.  There are a number of case in VB where this convention was not followed. Perhaps the ListBox and Form objects should have agreed on the standard for emptying their contents. Clear is used for the ListBox and CLS is used on the Form. The Debug object does not have a Clear Method.


Inheritance

Inheritance is the ability to create a new class from an existing class. VB has some limited ability in this area. The idea of inheritance is to create a base class and then create a new class that has all of the properties and methods of the base. The base is used as a model in this case and the new class can selectively override certain behaviors inherited from the base class.


Here is an example that incorporates some of these concepts

 


Object Oriented Programming

It is at this point that we begin to build our own classes so that they can be used to create objects based on that specific class model. Classes expose an interface to their clients. We see this in the form of the Properties and Methods associated with each class.

Components provide reusable code in the form of objects. An application that uses a component’s code, by creating objects and calling their properties and methods, is referred to as a client.

What is a class?

A class is blueprint or a plan for an object. When you create a class, you create a new type of variable. Your new class is a collection of variables and related functions that are referred to as class properties and methods.

Using the ListBox from the ToolBox as an example, you know that ListBoxes have ListCounts and can be Sorted and you can reference the ListIndex to determine if an item has been selected. These are the variables or characteristics or properties of the ListBox. You can AddItems and RemoveItems and Clear its contents. These are the functions or methods of the ListBox.

When you build a class, you build an object blueprint that includes the necessary properties and methods.

What is the difference between a class and an object?

An object is a particular instance of a class. When you add a ListBox to a Form, you are creating a specific instance of a ListBox type of control. You can have many instances of a ListBox in a project. Each is an object.

Building a Class Module

To add a class to a project use the
Project | Add Class Module

Give the class a name and save the Class Module.

You are ready to add the properties and methods to support the new type.

Click on Tools | Add Procedure

Add Procedure

Select Property to add a Class Property or Function or Sub to add a Class Method

Properties

As was mentioned before, a property is a characteristic of  the class. We tend to think of them as variables, but they are in fact procedures or functions that return a value to the client.

Adding a property called Width to a class will generate this code skeleton:

PropWidthA.jpg (9970 bytes) Public Property Get Width() As Variant


End Property

Public Property Let Width(ByVal vNewValue As Variant)


End Property

What happens behind the scenes is not exactly what the client sees. When the client references a Property value, what is really happening is that a call to the Property Get procedure is invokes. The Property Get procedure will return a value to the client. When the client changes a Property value, it is the Property Let procedure that does the work.

Methods

Adding a Sub or a Function will create a Class Method. This is a routine that will perform a service on behalf of the client.

For example, I have defines a Class with Property Width and Method Move. After creating an instance of the class, I can select the Properties and Methods just as I do after creating objects using the ToolBox

 

Class Properties

 

Class Initialize / Class Terminate

Class Initialize and Class Terminate are the Form Load and Form Unload of Classes. These are the hooks that are available to perform any setup / initialization and shutdown procedures required when your class object is created and when it goes out of scope.

Creating Objects from Classes


Example 1

Private cTemp as CTemperature

    Set cTemp  = New CTemperature

Class Initialize is run when the set statement executes

   Set cTemp = Nothing

Class Terminate will run when this is executed

 

Example 2

Private cTemp as New cTemperature

Class Initialize runs the first time the object is referenced. There is no Set statement needed to create the object instance. Class Terminate will run when the Object goes out of scope. If this is a Module Level variable, it will loose scope when the program terminates. If created in an event handler, it will go out of scope when the event handler terminates. The class instance can at any time be explicitly Set to Nothing, and this will also cause the Class Terminate to execute.

 

Example 3

Private cTemp as Object

    Set cTemp as New CTemperature

VB allows this generic creation of objects but it is not very efficient. This is related to what is known as binding the object. This example is an example of Late Binding. It is not until Run Time that VB can determine what Object will be created. This involves more overhead to manage this type of bind. Early Binding is demonstrated in the first two examples. The type of object to be created can be determined at design time

 

Class Exposure

The Public Properties and Methods of a class are said to describe "The Interface" to the class. A Class will be built, and then perhaps modified over time based on changes to the original specifications. It is important to NEVER remove a Public Property or Method. To do this will cause existing applications to fail when the class is replaced. The interface can often be expanded, as expansion will not cause the client to fail.

Think of what would happen if VB changed the .Text Property of the TextBox to .Caption.

Never remove Public Properties or Methods from a Class

Object Browser

The Object Browser allows you to see more that the Interface to the class. It allows you to see both Public and Private Properties and Methods.

A Property or a Method that is Private is not accessible from the client as that part of the interface is not exposed. It is important for a class to protect the internal variables from direct manipulation by the client.

Class Initialize and Class Terminate are always private as these functions control the setup and cleanup of each object that is created.

It would be improper for the client to invoke these private functions directly.

ObjectBrowser.jpg (20503 bytes)

 


A Temperature Conversion Class

Option Explicit
Private sngTemp As Single

Public Property Let Temperature(ByVal sngTempIn As Single)
  sngTemp = sngTempIn

End Property

Public Property Get Celsius() As Single
  Celsius = FtoC(sngTemp)

End Property

Public Property Get Fahrenheit() As Single
  Fahrenheit = CtoF(sngTemp)

End Property

Private Sub Class_Initialize()
  sngTemp = 0

End Sub

Private Function CtoF(sngCelsius As Single)
  CtoF = (9 / 5 * sngCelsius) + 32

End Function

Private Function FtoC(sngFahrenheit As Single)
  FtoC = 5 / 9 * (sngFahrenheit - 32)

End Function

Temperature Conversion 1


ActiveX DLLs

An ActiveX DLL is the standalone implementation of a class module. Creating and registering a .DLL on the workstation makes the services available to any application that knows the the interface. Start a new VB project and select ActiveX DLL and add the class module from the previous example to the project. Set the Instancing Property of the Project to MultiUse and you have easily converted the class to a standalone project.

Visual Basic makes it easy to develop and test ActiveX DLLs. You can run two instances of Visual Basic. First, run the ActiveX Server. Then start the second instance of VB and build the client portion as from the previous example. In the client application, select Project | References and select the class that has been registered on the workstation through the first Visual Basic project running the ActiveX project. The Server component is on the left and the client is on the right.

Notice that the name of the registered component is the Visual Basic Project file

Having both projects open and running at the same time makes the development and debugging process much easier that having to debug the server with a standalone client.

Make the DLL / Register the DLL

When the development is complete, the File | Make selection will build the .DLL file. Your client application will need to select Project | References again, but this time select the .DLL version of the class

ActiveX EXE

You can package the services of the class into an executable application. In general, you would build an ActiveX .EXE when the program needs to have a visual interaction with the user. It is a standalone application, that will also provide services to other clients.

Temperature Conversion 2