3

The LA Fox Developer Newsletter
October 1999
Inter-Object Communications
in Visual FoxPro
Part 2
By Steve Sawyer
(Ed. Note: Part 2 of a two part series begun last month.]

An alternative technique that avoids the problems of hard-coding object references is to provide an object with the object reference (as a character string) of an object that it must communicate with. Note that it doesn’t matter whether the object is instantiated as a child object (and therefore is referenced by its Name property) or as an object variable (and referenced by the variable name). A call to TYPE(<cOjbect_Reference_Name>) will return "0" in either case. It is simply important to make sure that what is passed is indeed the object reference of the object.

SET PROCEDURE TO MsgEx5 ADDITIVE
oTestObjecti =CREATEOBJECT(”Messager”,”oTestObject2”)
oTestObject2=CREATEOBJECT("Messager","oTestObject1")
oTestObject3CREATEOBJECT(”MeSSager”)
oTestObject3.SetTarge("oTestObject1 “)
oTestObject3.SendMessage()
oTestObjecti .SendMessage()
oTestObject2.SendMessage()

ListingS. Msg&S.PRG.

*_ PROGRAM MsgEx5.PRG
*_ Example 5
Demonstrates how an object can “address”
*_ a message to another object by storing the
*_ object reference of the target object a to a character-type property of the sending
*
object. The sending object can then use
*_ =EVALUATE() to send a message to the other
*_object.
DEFINE CLASS Messager AS custom

PROTECTED cTargetObject
cTargetObject =
FUNCTION mit
LPARAMETERS tcObjRefName
IF TYPE(”tcObjRefName”) = “C”
This.Setlarget(tcObjRefName)
ENDIF
ENDFUNC

FUNCTION SendMessage()
LOCAL IcSendComm
IF! EMPTY(”This.cTargetObject”)
IcSendComm = This.cTargetObject +
“.SayHeIIoO”
=EVALUATE(IcSendComm)
ENDIF
ENDFUNC

FUNCTION SayHello
=MessageBox(”Hi there! - My name is +
This.Name,O,”Messaging Example 5”)
ENDFUNC
FUNCTION SetTarget LPARAMETERS tcObjRefName
IF TYPE(”tcObjRefName”) = “C”
Thls.cTargetObject = tcObjRefName
ELSE
This.cTargetObject =
ENDIF
ENDFUNC

ENDDEflNE

The first two commands which instantiate Messager as oTestObjecti and oTest0bject2, also set a property (cTargetObject) in each object to point to one of the other object~s object reference variables. This is accomplished through the LPARAMETERS of the lnit() method, and its call to the Setlarget() method. This sets up the ability for the two objects to send messages to each other. A third object is instantiated from the Messager class as oTestObject3, and its target object is then set as a separate step in the next command which calls the object’s Setlarget() method, establishing an ability to send messages to oTestObjecti.

The coding technique that allows the runtime substitution of an object instance variable is the macro substitution in the fourth and fifth lines of the SendMessageO method:

IcSendComm = This.cTargetObject + “.SayHeIIoO” =EVALUATE(IcSendComm)

The message boxes produced in response to the last three command-window lines show that the objects responding (“receiving” the message) are oTestObjecti (whose ‘name’ is uMessagerl oTest0bject2 (“Messager2”), and (again) oTestObjecti (“Messagerl “). Note that an advantage of this method of “assignment” or “registration” of a target object can be accomplished either at design-time or run-time, and that each object can be instantiated without concern for whether its target object is yet instantiated. To avoid a run-time error, care must be taken that the SendMessage() is not called until the target object exists. When implementing a messaging strategy, it may be advisable to check for the existence of the target object before attempting to send a message to it, and provide steps to take if the target object does not exist, as is done in the next example.

The next example demonstrates another method of introducing flexibility into our applications with regard to object referencing. Examine the code in example 6 which defines a single class, “Messager”, which has a property oTargetObject that, instead of being a character-type property holding the object reference of the target object as a character string, is an object-type property, that actually holds another reference to the object. Recall that if we instantiate an object:

x = CREATEOBJECT(”foo”)

and then store that object to another variable:

(Con’t, page 4)
Page 3

3