* TryCreateObject.prg - example for safe object creation without TRY ... CATCH lparameters cClass, roInstance roInstance = .null. = createobject([DoIt], m.cClass, @m.roInstance) return vartype(m.roInstance) == "O" ******************************************************************************* define class DoIt as relation function Init (cClass, roInstance) roInstance = createobject(m.cClass) return .f. procedure Error (nError, cMethod, nLine) return to Init enddefine(VFP3 and VFP5 need some suitable definition for an ersatz VARTYPE(), of course, because that function was introduced in VFP6.)
local oHandler do case case TryCreateObject(SynthesizeHandlerClassName1(m.uArg), @m.oHandler) case TryCreateObject(SynthesizeHandlerClassName2(m.uArg), @m.oHandler) case TryCreateObject([CFallbackHandler], @m.oHandler) otherwise ? "WTF, not even the default class is there?" * ... endcase oHandler.Handle(whatever)The AVCX() approach requires a more work but I think on the whole it is more solid and you don't have to be as careful with the classnames as in the brute force "query" method. On the other hand the AVCX() solution is a bit more rigid in that your factory needs a list of VCXen to query but I do not think that this is an unsurmountable problem. ;)