8

The LA Fox Developer Newsletter
September 1999
Communications (Con't from page 7)
parent object’s object reference is, yet it can successfully send a
message to the object based on MessageParent [instantiated as
oParent, in our example call] to have it execute its SayHello()
method.)

The “Parent” keyword is also useful when sending a message to a “sibling” object, that is, another object that is also contained within an object’s parent object. However, in this case, one must either explicitly name the receiving object, or employ one of the methods to be discussed later. The following code entered into the command window will demonstrate the classes defined in Code Listing 3:

SET PROCEDURE TO MsgEx3 ADDITIVE
oParent=CREATEOBJECT(”MessageParent”)
oParent.Childl Send Message()
oParent.Child2.SendMessage()

Listing 3. MsgEx3. PRG.
* PROGRAM MsgEx3.PRG Example 3
Demonstrates how a child object can reference
*_ a method of another child object, but must know the name of the other child object.
*_ Also demonstrates that an object’s name is
*_ useful in the context of a container
** object, or in the context of another object sharing In the parent-child relationship.

DEFINE CLASS MessageParent AS custom

ADD OBJECT Childi AS MessageChild ADD OBJECT Child2 AS MessageChild name = “Parent_Object”

ENDDEFINE

DEFINE CLASS MessageChild AS custom

FUNCTION SendMessage
DO CASE
CASE This.Name = “Childi”
This.Parent.Child2.SayHeIIo()
CASE This.Name = “Child2”
This.ParentChildl .SayHelIo()
ENDCASE
ENDFUNC

FUNCTION SayHello
=MessageBox(”HeIlo! - This is "+
Thls.Name,O,”Messaging Example 3”)
ENDFUNC

ENDDEF1NE

(Note: The SendMessage method in the MessageChild class uses the THIS keyword to determine (at run-time) the object’s name, and uses this Information to avoid sending a message to it’s own SayHello() method. Instead, it sends a message to its sibling object.)

In Listing 4, as in Listing 3, we create two instances of the same class definition, but the object reference (“oTestObject2”) of the destination object is “hard-coded” into the SendMessage() method. This illustrates the problems with hard Coding object references; calling the SendMessage() method of either
oTestObjecti oroTestObject2 results in the SayHello() method of oTestObject2 being called.

SET PROCEDURE TO MsgEx4
oTestObjecti CREATEOBJECT(”Messager”)
oTestObject2=CREATEOBJECT(”Messager”)
oTestObjecti .SendMessage()
oTestObject2.SendMessage()
*
PROGRAM MsgEx4.PRG
~- Example 4
*
Demonstrates how an object can “address”
*_ a message to another object by hard-coding the
*_ instance variable name of the other object,
*_ and demonstrates the sending object’s
*
dependence on correctly specifying the object
*_ reference ofthe receiving object

DEFINE CLASS Messager AS custom

FUNCTION SendMessage()
oTestObject2.SayHelIo()
ENDFUNC

FUNCTION SayHello
=MessageBox(”Hi therel - My name is +
This.Name,O,”Messaging Example 4”)
ENDFUNC

ENDDEFINE

Note that the MessageBox reports “My name is Messager2” when the Send Message() method is called for either object.
End-Part1


(Ed. Note: Steve Sawyer is Director of Technology for Kirtland Associates, a company specializing in custom database applications. Steve is a Microsoft Certified Professional, a Microsoft MVP, and a contributing editor forAdvisor Media, editing the monthly Tips Tricks and Traps column in FoxPro Advisor. He is co-author (with Jim Booth) of “Effective Techniques forApplication Development with Visual FoxPro 6.0 (Hentzenwerke Publishing, 1998). Steve is a founding member and president of the DetroitArea Fox User Group, and resides in suburban Detroit, Ml.]

(One last Ed.Note: For those of you who absolutely can’t wait for the final installment next month, point your browser to the LA Fox website at http://www.lafox.org and check out the downloads section for 'message.zip".}


Limiting... (Con't from page 2)
Add a 12 character field to your user table called TextFil When a user logs in, run the following code:
#DEFINE MAX_USERS 10

*
Check how many users are logged in
InUserCount = 0

(Con't page 9)
Page 6

8