Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is there an 'IsClass()' function?
Message
From
09/11/2003 18:26:35
 
 
To
09/11/2003 12:09:49
General information
Forum:
Visual FoxPro
Category:
Classes - VCX
Miscellaneous
Thread ID:
00847972
Message ID:
00848126
Views:
18
You could factor your current approach into a little function of its own, so as not to clutter your factory object with spaghetti error handling related to semi-expected object creation failures. Basically, the idea is to set up an exception frame in the customary manner for VFP3 .. VFP7:
* 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.)

Then you could combine test for existence of the class and creation of an instance into one neat function call:
   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. ;)
Previous
Reply
Map
View

Click here to load this message in the networking platform