What is a Class? – Basic understanding of a Class Module – Chapter3

How to implement Properties, Methods and Events on a Class

Creating a Property

You guys might feel a little tired with all this theory. So this time, let's actually make a simple example class called PHONE.

Open the VBE (Visual Basic Editor), then insert a Class Module from Insert > Class Module menu. Next, on the Properties Window, change the name of the inserted class Class1 to PHONE. Now, you have a Class Module named PHONE. Now, you have a Class Module named PHONE.

As the next step, let's assign a property named Number. In order to set a value to the property, use the Property Let statement.

The content of the of property is indeed a Variable so to keep the value of a property, we must use a variable. Normally, a Private type variable is used in the Class Module. It is concerned with scope level, but so far, to avoid getting you confused, just remember it as it is. For your information the prefix m stands for a variable declared as a module level variable.

Declare a variable named m_strNumber as a Private type. Since the Phone Number is string, declare it as a string type. This variable will be used for keeping the value in this Class Module.

'A Class Module named PHONE
Private m_strNumber As String

Next, using the Property Let statement, make a procedure for setting a value to this property.

Property Let Number(ByVal newvalue As String)
    m_strNumber = newvalue
End Property

The above procedure will pass a value in the argument “newvalue” to a private variable m_strNumber, and the value will be kept in the m_strNumber. To get a value from the property, use Property Get statement.

Property Get Number() As String
    Number = m_strNumber
End Property

The above code would pull the value from a private variable m_strNumber. It looks like a Function procedure.

From above example, you can see a Property is handled as a variable inside of the class.

Finally, you can use the Number property on the PHONE class. So let’s use this property from a standard module.

Insert a standard module then enter the following code.

Click here to DOWNLOAD a sample workbook.

'As I wrote, to use a Class from a standard module,
'we need an object variable as a relay object,
'so declare an object variable named MyPhone in the top of module.
Dim MyPhone As PHONE

Sub Class_Initialize()
'When set a property, at first we need to create
'an instance of Phone class.
    Set MyPhone = New PHONE
End Sub

Sub Test_Set_Property()
    If MyPhone Is Nothing Then
        Call Class_Initialize
    End If
    'You can omit Let Statment
    Let MyPhone.Number = "1234"
End Sub

Sub Test_Get_Property()
    MsgBox MyPhone.Number
End Sub

Please have a look at the code shown in the standard module part. Sub Test_Set_Property set a Number property to the PHONE Class. To handle a class from a standard module, need to create an instance of the Class. So if the Object MyPhone is not created as yet, you need to tell VBA what the MyPhone is. After that, you can use a created property like built-in properties.

To be continued to the next chapter.

| HOME |
Copyright © cellmasters.net - colo's junk room All Right Reserved
Tips and Information about Microsoft Excel|Masaru Kaji aka Colo