Foxpro ReturnObject

Function to prevent problems during object creation to crash your program. When problems arise, your program might be unable to continue, but at least you can show the user what went wrong instead of an uninformative crash warning.

* ReturnObject 
* 2011.05.31 start
* 2012.07.06 parameters added
* Tries to create an object and return this. If object creation failed, a false is returned
* Parameters: Class to instantiate object from, max 2 parameters
FUNCTION ReturnObject( tcClass,tuPar1,tuPar2)
 LOCAL loReturn,lnCount,loError
 lnCount = PCOUNT()
 TRY 
  DO CASE 
   CASE lnCount = 1
    loReturn = CREATEOBJECT(tcClass,tuPar1)
   CASE lnCount = 2
    loReturn = CREATEOBJECT(tcClass,tuPar1,tuPar2)
   OTHERWISE 
    loReturn = CREATEOBJECT(tcClass)
  ENDCASE 
 CATCH TO loError
  loReturn = .f.
 ENDTRY 
 RETURN loReturn 
ENDFUNC 

One Reply to “Foxpro ReturnObject”

Leave a Reply

Your email address will not be published. Required fields are marked *