Gary,
If you're using the latest version of the Framework, business object's have a new method called "GetBizObject" that accepts a single parameter--a business object class name. This method looks for the specified business object in the following places:
1. In the RefBizObj of the current business object (indicating a child business object)
2. In the form's RefBizObj
3. If it doesn't find the business object in either of these places, it automatically creates an instance of the requested business object for you.
Below, is an updated version of this method that will be released in the next version of the Framework...I suggest you replace your current code with the code listed here. This updated method now adds an "lCreatedNewInstance" property to the business object at runtime if it couldn't find an existing instance and had to create a new one. This allows your code that calls the business object to release the business object if the lCreatedNewInstance property is set to True...this allows you to avoid dangling object references.
Here's the updated method code:
**************************
LPARAMETERS tcClassName
LOCAL loReturnObj, llCreate
IF TYPE("This.RefBizObj") == "O"
loReturnObj = This.RefBizObj.Get(tcClassName)
ENDIF
IF VARTYPE(loReturnObj) != "O" AND This.IsFormHost() AND ;
TYPE("ThisForm.RefBizObj") == "O"
loReturnObj = ThisForm.RefBizObj.Get(tcClassName)
ENDIF
IF VARTYPE(loReturnObj) != "O"
loReturnObj = CREATEOBJECT(tcClassName)
llCreate = .T.
ENDIF
IF NOT PEMSTATUS(loReturnObj,"lCreatedNewInstance",5)
loReturnObj.AddProperty("lCreatedNewInstance", llCreate)
ENDIF
RETURN loReturnObj
**************************
Here's an example of how to use this new method:
**************************
LOCAL loMyObject
loMyObject = This.GetBizbject("MyClassName")
*** place code here that uses the object
IF loMyObject.lCreatedNewInstance && Release the object if it was created by GetBizObject()
loMyObject.Release()
ENDIF
**************************
Regards,
Kevin McNeish
Eight-Time .NET MVP
VFP and iOS Author, Speaker & Trainer
Oak Leaf Enterprises, Inc.
Chief Architect, MM Framework
http://www.oakleafsd.com