Class modules are more like templates. You use class modules to describe an object.
Here's one I made earlier...
<class module>
Private m_strID As String
Private m_lngFreq As Long
Private Sub Class_Initialize()
'Called when object is created
m_lngFreq = 0
End Sub
Public Property Get ID() As String
ID = m_strID
End Property
Public Property Let ID(ByVal strID)
m_strID = strID
End Property
Public Sub IncFreq()
m_lngFreq = m_lngFreq + 1
End Sub
Public Property Get Freq() As Long
Freq = m_lngFreq
End Property
'-----------------------------------
The class module describes the object, there are two properties ID and Freq.
ID is read-write as there is a GET and a LET property. Freq is read-only as there is no LET property. There is a third type of property which is SET (not in example) which is used for objects. The set property would replace the let property, but you should still use a get property.
You could just have used -
public m_lngFreq
- this would allow the user of the object to manipulate the variable (property) at will. However, by hiding the data away from the user (or calling program), you can get a degree of control impossible with variables alone.
Again, class modules provide a template for an object. To create an object based on the class you need to instantiate (which just means create an instance) the object.
dim objFreqCounter as clsFreqCounter
'assuming clsFreqCounter is the class module name...
set objFreqCounter as New clsFreqCounter
The first line 'dim...' dimensions a variable to hold your object, it doesn't actually create that object. You use the both the Set and New keywords to create a object. Without using the New keyword, you simply point the variable to an existing object.
dim objFreqCounter as clsFreqCounter
dim objFreqCounter2 as clsFreqCounter
'assuming clsFreqCounter is the class module name...
set objFreqCounter as New clsFreqCounter
set objFreqCounter2=objFreqCounter
At the end of this code, there is only one instance of the object.
While we're on the subject of references, you should release references explicitly, when you don't want them anymore.
set objfreqcounter=nothing
This releases the reference to the object. However, there is still another variable with a reference to the object, an that also needs to be released before the object is cleared from memory
set objfreqcounter2=nothing
Now that there are no references to the object, the object is destroyed, freeing up memory.
There was a sub in the class module, incFreq. Subs and functions within a class module are known as 'methods'.
Going back to your question (finally...) class modules are templates for objects (frequently used analogy is a cookie cutter), standard modules are holders for variables,subs and functions and can't be used to describe objects.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.