[AccessD] CLSMSG DEMO

jwcolby jwcolby at colbyconsulting.com
Fri Feb 13 08:37:12 CST 2009


In this lecture we will learn to create a class, build a module to initialize the class, and then 
test the class functionality.  The class we will build will use clsDemoMsg to send communication 
events back and forth between the class instances.  The purpose of the lecture is threefold, to 
practice building new classes, to practice setting up and tearing down classes outside of forms, and 
to demonstrate clsMsg.

•	In the demo database, click Insert / Class Module.
•	Immediately save a clsMsgDemo
•	In the header type in the following code:

Private mstrName As String

This creates a private name variable for the class instance.  Remember that we can create as many 
instances of a class as we want, and for the purposes of this demo we will create three.  Each 
instance will need a name so that it can identify itself to other class instances, and so that it 
can know when a message is sent to itself.

Private WithEvents mclsMsg As clsMsg

This dimensions WithEvents a private variable for a clsMsg, informing the class that we will be 
sinking events for clsMsg.

•	Drop down the left combo box and select class.  This will create the Class_Initialize event stub.
•	Drop down the right combo box and select Terminate.  That will create the terminate event
•	In the Initialize and terminate event stubs type the following code:

Private Sub Class_Initialize()
     Set mclsMsg = cMsg()
End Sub

Private Sub Class_Terminate()
     Set mclsMsg = Nothing
End Sub

This code calls cMsg() which initializes the message class if it is not already initialized, then 
returns a pointer to the initialized clsMsg, which we then store into mclsMsg.  We are now ready to 
send messages on the message channel we have set up.  The terminate event stub cleans up out mclsMsg 
pointer.  We need to make it a habit to clean up any objects that we use.

•	Create property statements to expose the mstrNname variable.

Property Get pName() As String
     pName = mstrName
End Property

Property Let pName(lstrName As String)
     mstrName = lstrName
End Property

Classes expose their internal private variables through property statements.  Property Get and 
Property Let statements return the internal variable and initialize the variable respectively.  It 
is possible to simply expose the variables by using a Public in the place of Private, but I try hard 
never to do that.  The Get and Let statements allow you to control access to the variables, perform 
processing if needed as you get and set the internal variables, and set up read only or write only 
variables by using only the Get or Let statements.  You will run into programmers that will argue 
vehemently that get / let statements are a waste of time, but this is my lecture and I use them myself.

I like to prefix all of my properties with a lower case p, just so that they group together.  I also 
like to prefix my methods with a lower case m, just so that they group together.

•	Drop down the left combo and select mclsMsg.  This will create the Message event sink stub.
•	In the event stub, type in the following code:

Private Sub mclsMsg_Message(varFrom As Variant, varTo As Variant, varSubj As Variant, varMsg As Variant)
     If varTo = mstrName Then
         Dim strMsg As String
         strMsg = "Hello, the class instance " & mstrName & " is sinking this message." & vbCrLf
         strMsg = strMsg & "The entire message sent to me is:" & vbCrLf
         strMsg = strMsg & "From: " & varFrom & vbCrLf
         strMsg = strMsg & "To: " & varTo & vbCrLf
         strMsg = strMsg & "Subject: " & varSubj & vbCrLf
         strMsg = strMsg & "Msg: " & varMsg & vbCrLf
         MsgBox strMsg, vbOKOnly, "MESSAGE FROM" & varFrom
     End If
End Sub

This is the event sink for mclsMsg, and allows us to do something with messages that mclsMsg is 
sending out to whoever is listening.  Since we opnly want to respond to messages sent to this 
specific instance of the class, we test varTo against mstrNname which is the name of this class 
instance.  If the message is to us, then we do something with it.  In this case we are just going to 
build up a message and pop it up in a message box.

•	Next we will create a method that allows this class to send a message on the message channel. 
Underneath the event stub we just created type in the following code:

Public Function mSendMsg(varTo As Variant, varSubj As Variant, varMsg As Variant)
     mclsMsg.Send mstrName, varTo, varSubj, varMsg
End Function

This method is nothing more than a wrapper to the classes .Send method, and simply passes in the 
parameters to the mclsMsg.  Notice that it sends mstrName as the From variable, in other words this 
class knows it’s name and uses that to automatically tell the message recipient who the message is from.

In this lecture we have created a new class, told it to use a message class, created properties to 
allow getting and setting the class instance name, created an event sink for the message class and 
inside of that event sink used the event to do something.  Finally we created a method of this class 
to allow us to use this class from the outside, cause the class to take an action, in this case just 
to send a message.  In the next lecture we will actually use this class to demonstrate how to use 
classes that are free standing.

-- 
John W. Colby
www.ColbyConsulting.com



More information about the AccessD mailing list