Creating an Object
You need to set up the actual object for items within the collection first because this has to be
used in the construction of theCollectionsobject. TheMyPNameobject will have one
property, the name of the object calledPName. Remember that a property holds a value for
something within that object—in this case, the actual name.
You first need to create a variable to hold the property value, which you do in the
declarations section of the module:
Private mPname As String
This sets up a string variable calledmPname. Notice that it is private and cannot be seen as
part of the object. This is because it is a working variable within the class module and is not
for use by the user. Also, the name must be different from the actual property name the user
can see, which is why themis added at the front.
Next, you need to set up code to control access to the property. You need aProperty Let
statement and aProperty Getstatement, and the statements must be public—otherwise, you
will not be able to view them within the object. These statements effectively read and write
values to the property you are setting up:
Public Property Let Pname(vdata As String)
mPname = vdata
End Property
The parametervdatarepresents the value being passed to the property within theLetstatement.
Public Property Get Pname() As String
Pname = mPname
End Property
It is very important that theLetandGetproperty statements have the same name; otherwise,
you will be able to read a property (Get) but not write back to it (Let), or you will be able to
write to it (Let) but not read it back (Get).
TheLetproperty uses a string variable to transfer the data into yourmPnamevariable.
TheGetproperty sets the property value to whatever is held in yourmPnamevariable.
You can use Insert | Procedure from the code menu to create a code skeleton for a property,
which opens the Add Procedure dialog shown in Figure 21-1. This automatically writes the
LetandGetproperty statements and ensures that they both have the same name.
You have now set up an object calledPname. This object sits in a collection calledPNames,
just as theWorksheetobject sits in a collection calledWorksheets. TheMyPNamescollection
is created in the next section.
Chapter 21: Class Modules 267