Visual Basic Concepts
Creating Properties and Methods for the Thing Class
You create properties for a class by adding public variables and property procedures to the class module. You create methods for a class by adding Public Sub and Public Function procedures to the class module. The following step-by-step procedures create two properties and one method for the Thing class.
The Name property is a string that can be retrieved and set by client applications.
The read-only DebugID property returns a sequence number that shows the order in which Thing objects were created. This is useful for debugging.
The ReverseName method reverses the order of the letters in the Name property.
Note This topic is part of a series that walks you through creating a sample ActiveX DLL. It begins with the topic Creating an ActiveX DLL.
To create the Name property
Add the following code to the Declarations section of the Thing class module:
Option Explicit Public Name As String
The variable Name
becomes a property of the Thing class because it’s declared Public.
Important Don’t confuse the Name property you’re creating here with the Name property of the class module. The Name property of the class module allows you to specify the class name (Thing) at design time; it’s not available at run time.
To create the read-only DebugID property
Add the following code to the Declarations section of the Thing class module:
' To store the value of the DebugID property. Private mlngDebugID As Long
From the Tools menu, choose Add Procedure to open the AddProcedure dialog box. In the Name box, type DebugID. Click Property and Public, then click OK.
In the Code window, delete the Property Let procedure and change the Property Get procedure as follows:
Public Property Get DebugID() As Long DebugID = mlngDebugID End Property
The purpose of a Property Let procedure is to allow users to assign a new value to the DebugID property. Deleting it makes the property read-only.
Tip Because property procedures come in two parts, you may find it easier to work with them in Full Module View. You can toggle between Procedure View and Full Module View using the buttons in the bottom left corner of the Code window.
The variable mlngDebugID
is a private data member which is used to store the value of the DebugID property. Because it’s declared Private, it’s not visible to client applications, and thus cannot be changed by clients. This is an example of encapsulation, discussed in detail in "Classes: Putting User-Defined Types and Procedures Together," in the Visual Basic Programmer’s Guide.
The Property Get procedure returns the value of the private variable, allowing clients to read the property value using code like the following:
Private Sub Command1_Click()
Dim t As ThingDemo.Thing
Set t = New ThingDemo.Thing
MsgBox t.DebugID
End Sub
To create the ReverseName method
On the Tools menu, click Add Procedure to get the Add Procedure dialog box. In the Name box, type ReverseName. Click Sub and Public, then click OK.
Enter the following code in the new Sub procedure of the Thing class module:
Public Sub ReverseName() Dim intCt As Integer Dim strNew As String For intCt = 1 To Len(Name) strNew = Mid$(Name, intCt, 1) & strNew Next Name = strNew End Sub
Note Remember that the Name property you’ve just created — and used in the ReverseName method — is not the same as the Name property of the class module. Unlike the Name property you created, the class module’s Name property is not available at run time.
Step by Step
This topic is part of a series that walks you through creating a sample ActiveX DLL.
To | See |
Go to the next step | Adding Code for Initialize and Terminate Events |
Start from the beginning | Creating an ActiveX DLL. |