General information
Category:
Coding, syntax & commands
Randy --
A number of suggestions here:
1. If your instantiated object is contained soley in a method routine as some local varibale (i.e., loMyObject), then the test involves asking ...
(( type("loMyObject") == "O" and not isnull(loMyObject) ))
2. If your instantiated object is more global then you are best served by storing the object reference into some class property (THIS.ioMyObject) or an object array property (THIS.iaMyObjectList[x]). In either event, you should construct a method whose responsibility it is to know how to see if that object is instantiated. Thus, for a property you might have the following:
THIS.qlMyObjectExists where the method code is:
local llMyObjectExists, loMyObject
llMyObjectExists = .f.
loMyObject = THIS.ioMyObject
if (( type("loMyObject") == "O" and not isnull(loMyObject) ))
llMyObjectExists = .t.
else
llMyObjectExists = .f.
endif
return llMyObjectExists
If the object is stored in an array (assume 1 dimensional array populated with object references where each object has a query method to return a coded ID):
local llMyObjectExists, loMyObject, lnRow, lnNumberOfRows
local loTestObject, lcObjectID
local array laMyObjectList
lcObjectID = "SOME-ID"
llMyObjectExists = .f.
acopy(THIS.iaMyObjectList, laMyObjectList)
lnNumberOfRows = alen(laMyObjectList,1)
for lnRow = 1 to lnNumberOfRows
loTestObject = laMyObjectList[lnRow]
if not llMyObjectExists
llMyObjectExists = (( loTestObject.qcObjectID() = lcObjectID ))
endif
endfor
return llMyObjectExists
I am sorry that this is a lot of code, but the idea is simple. Create a query method (routine) that contains the knowledge of "how" to answer the "Does my object exist?" question. Then, ALL the objects in your system can have a "one-stop-shop" place to ask the question and get the definitive answer. Thus, all over your code where you need to ask the question you will say:
llTheAnswerIs = THIS.qlMyObjectExists()
Furthermore, notice that the method "qlMyObjectExists" hides whether the object reference is stored in a property or in an object array property. The client (calling routine) does not need to care about the exact implementation of storing the object reference. This also means that if you change how you store the reference (i.e., switch from property to array), the rest of your client code that uses the qlMyObjectExists method will not need to be recoded to manage the change.
Best Regards,
Larry
Previous
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only