4

The LA Fox Developer Newsletter
October 1999
Communications (Con’t from page 3)

y=x

we only have one object, but two references to the same object. As a result we can call a Print() method of an object based on the class “Foo” by either of these lines of code:

x.Print()
y.Print()

This holds true not only for conventional memory variables, but for object properties (which are simply variables scoped to the object) as well.

In the next example I include a new method, TalkToMeO, that will allow an object to “register itself with another object as the recipient of messages from that object. TalkToMe() simply calls the SetTarget() method, but passes the keyword THIS as the function argument. The object is actually passing itself by reference to another object, to enable that object to communicate back. The following lines will instantiate the Messager class in example six and demonstrate its capabilities.

SET PROCEDURE TO MsgEx6.PRG
Create Stan
oStanleyObject=CREATEOBJECT(”Messager”)
Create OIlie, target of his messages is Stan
oOliverObject=CREATEOBJECT(”Messager”,oStanleyobject)
a Set target of Stan’s messages to Ollie oStanleyObject.SetTarget(oOhverObject)
oStanleyObject.name = “Stanley” oOliverObject.name = “Oliver” a Tell Ollie to say hello to Stan oStanleyObject.SendMessage()
a Tell Stan to say hello to OHie oOliverObject.SendMessage()
a Create Charlie, target of his messages is Stan oCharlieObjectCREATEOBJECT(”Messager”,oStanleyObject)
Tell Stan to talk to Charlie oCharlieObject.TalkToMe()
oCharlieObject.Name = “Charlie” a Have Stan say hello to charlie oCharlieObject.SendMessage()

Listing 6. MsgEr6.PRG.
a PROGRAM MsgEx6.PRG
*
Example 6
Demonstrates how an object can “address”
a a message to another object by sto~1ng a
*
reference to the target object’s variable name
as a property of the sending object
*
The sending object can then use normal
*
object syntax to communicate with the target
object.
a Also demonstrates how an object can “register”
a.
itself with another, so that that object can
send messages back.

DEFINE CLASS Messager AS custom

PROTECTED oTargetObject
oTargetObject = .NULL.

FUNCTION Init
LPARAMETERS toObject
This.SetTarget(toObject)
ENDFUNC

FUNCTION SendMessage()
IF
TYPE(”This.oTargetObject”) = “0”
AND I ISNULL(This.oTargetObject)
This.oTargetObject.SayHello()
ENDIF
ENDFUNC

FUNCTION SayHello
=MessageBox(”Hi there” +;
This.oTargetObject.Name +;
“I- My name is “+ This.Name,O,;
“Messaging Example 6”)
ENDFUNC

FUNCTION SetTarget
LPARAMETERS toObject
IF
TYPE(”to0bject”) = “0” AND!;
ISNULL(toObject)
This.oTargetObject = toObject
ELSE
This.olargetObject = .NULL.
ENDIF
ENDFUNC

FUNCTION TaIkToMe
IF
TYPE(”This.oTargetObject”) = “0”
AND I ISNULL(This.oTargetObject)
This.oTargetObject.SetTarget(This)
ENDIF
ENDFUNC

ENDDEF1NE


This example shows 1) that it is possible to establish or change an object as a messaging recipient at run-time, 2) that it is possible for an object to determine who it is receiving a message from, and therefore 3) to set up a two-way communication between two objects. In many cases, a formal two-way “conversation” is not necessary, as an object’s methods can return values (like any other function) to the calling object, which is still another way that information can be passed between objects. This is not really “messaging”, but is simpler than setting up two-way messaging capabilities, and may be all that is needed for recipient-to-sender communications. However, there may be situations in which it may be necessary to set up more complex “conversations”.

Another technique incorporated in this version of the Messager class is checking for the existence of the target object before sending a message to that object. Note that I use both TYPE() and ISNULLO. This is because it is possible to destroy an object without destroying its reference. In such a case the object reference variable will still have a TYPE() of”O”, but its value will be .NULL. To see this in operation, Create a class AS CUSTOM, and include a ReleaseO method. This method will have a single line:

RELEASE This

Instantiate the object using CREATEOBJECTO, then call its Release() method. The object will be destroyed, but
(Con’t page 5)
Page 4

4