Class Module Properties

Although a Class’ properties can be implemented as public variables, Visual Basic provides the Property Let and Property Get procedures for accessing them. The proper method of implementing a property in your Class is to’ add the following procedures:

Private m_Int Property
Public Property Let IntProperty(new Value As Integer)
m_IntProperty = new Value
End Property
Public property.Get IntProperty() As Integer
IntProperty = m_IntProperty
End Property

“the private variable mJInt Property is local copy of the property’s value Every time an application sets this property’s value the Property Let procedure is automatically invoked and the new value specified is passed as argument. In other words the line:

!ntProperty = 100

invokes the following procedure in your Class:

Property Let IntProperty(l00)

Assignments to property values invoke a Property Let procedure (one with the same name as the property) to be executed. The procedure’s code can validate the new value and either accept it (and assign it to a local variable) or reject it. Likewise when the host application requests the value of the Int Property property the value of the local variable mJntProperty is passed to it

In the Property Let procedure you can insert all the validation code you see fit. For example here’s how you would implement an Age property:

Private m_Age
Public Property Let Age(newValue As Integer)
Select ase newValue
Case < 0: m_Age – 0
Case > 100 : mLAge – 100
Else m-Age-newValue
End Select
End Property
Public Property Get Agee) As Integer
IntProperty – m-Age
End Property

If the application specifies an invalid age value the Class takes some action that will prevent some serious errors later in the code. The amount of validation code depends on the nature of the property and you can insert as little or as much as you like.

You can also raise errors from with in your Property let procedure which can be trapped. by the application’s code. For example you could implement the Age property as follows:

Public Property Let Age(new Value As Integer)
If newValue < 0 or newValue ~ 100 Then
Err.Raise 1999. _
‘Invalid Age. It must be greater than _
zero and less than 100’
Else
m_Age – newValue
End Property

The error is passed back to the application and it will either generate a run time error or it will be trapped by the application’s code. You will find more information on raising errors from within a Class in the section “Raising Errors in a Class.”

You may think of displaying a message with the MsgBox() function from within the procedure’s code. This isn’t such a good idea for two reasons:

1. The message box may not be displayed on the user’s monitor. If the Class resides on a server and is used remotely by an application on a network (or the internet) he will never get a chance to click the OK button and as a result the application will freeze.
2. Since the error isn’t produced by the application’s code you can’t really offer the user descriptive information, just a generic message like “Please enter a valid value.” This action must take place in the application’s code and the best method of reacting to errors in Classes is to raise errors to the calling application and let the application handle it.

However, it’s possible to display message boxes from within a Class or even bring up an entire Form or dialog box and request user input, as long as you keep in mind that the Class’ visible user interface may become invisible if the Class is executed remotely.

Some Classes provide read-only properties. The simplest method of implementing a read-only property is to omit the Property Let procedure as we have done with the .c timer Class You can also raise a trappable run time error which the host application can intercept and then act accordingly. You will see how to raise errors
from within your Class later in this chapter.

Creating Objed Variables

In the last chapter, you learned how to access objects through object variables. You also learned two methods for declaring object variables:

1. Declare an object variable with the New keyword: Private object Var As New object Type
2. Declare an abject variable, and then set it to the object you want to access:

Private object Var.As objectType
Set objectVar New objectType

Both methods require that Visual Basic knows the object’s type

You also would expect Visual Basic to know about Forms, controls, and other types of built-in objects, but what about your custom objects? Visual Basic doesn’t know anything about ~ objects you create, so you must tell it that’ a specific project must access an object by adding a reference to this object to your project. For example, if you want to add the C timer Class to a project, open the Project menu and select References. In the References dialog box that appears, locate the object you want to reference from within your code, and click ‘OK.

If you open the Object Browser window after adding a new reference to your project, you will see the Type library of the recently added object. Select it in the Type Library box to see the Classes it exposes in the Classes pane. The C Timer project exposes a single Class, the c Tuner Class. Click the name of the Class to see-its members.

So unless you ‘add a reference to a specific object to your project you won’t be able to declare variables of this type. There’s also a third method of declaring object variables, the Create Object Q function which requires you to supply the application and Class name. First declare an object variable, and then assign an object instance to it:

,
Private TMR As Object
Set TMR = Create Object Timer Project C Timer·)

The, Set statement is usually placed in the Form_Load event or in the procedure that needs to access the object. you can change the declaration of the object variable TMR in the test application with the Create Object method shown. The rest of the code remains the same.

Scroll to Top