Page 1 of 1

Generic ActiveX methods via OnError

Posted: Mon Sep 11, 2006 8:01 am
by Brian Hays
Antonio:
I'm creating a wrapper class for a commercial ActiveX control.
Rather than code a Method for each function, I created the OnError handler below to attempt to post any unknown message as a call to the actual control. It seems to work, but there are a couple issues:

How can I handle passing non-string args? In PRG code, can I "push items onto the stack" and exec the Do() method?
Is there a better way to do this, or do you see a lurking problem?

Code: Select all

METHOD OnError( ... ) CLASS XXXView
static lInErrorAlready := .f.
local cMsg   := __GetMessage(), cCmd, x
private oSelf := Self

   IF lInErrorAlready
      _ClsSetError( _GenError( 1004, ::ClassName(), cMsg  ) )
   ELSE
      lInErrorAlready := .t.
      cCmd := "oSelf:Do('"+ cMsg
      FOR EACH x IN HB_aParams()
          cCmd += "','" + x
      NEXT
      cCmd += "')"
      &cCmd

   ENDIF
   lInErrorAlready := .F.

RETURN nil

Posted: Mon Sep 11, 2006 1:07 pm
by Antonio Linares
Brian,

We prefeer to avoid macros, and this way you get it working for any type of parameters (though it is limited to a certain amount of them, but you can increase it to 10 params or more):

Code: Select all

METHOD OnError( u1, u2, u3, u4, u5 ) CLASS XXXView 

   static lInErrorAlready := .f. 

   local cMsg := __GetMessage(), uRet

   IF lInErrorAlready 
      _ClsSetError( _GenError( 1004, ::ClassName(), cMsg  ) ) 
   ELSE 
      lInErrorAlready := .t. 
      do case
         case PCount() == 0
               uRet = ::Do( cMsg )

        case PCount() == 1
               uRet = ::Do( cMsg, u1 )

        case PCount() == 2
               uRet = ::Do( cMsg, u1, u2 )

        case PCount() == 3
               uRet = ::Do( cMsg, u1, u2, u3 )

        case PCount() == 4
               uRet = ::Do( cMsg, u1, u2, u3, u4 )

        case PCount() == 5
               uRet = ::Do( cMsg, u1, u2, u3, u4, u5 )

        otherwise
              MsgAlert( "More cases needed" )
     endcase

   ENDIF 
   lInErrorAlready := .F. 

RETURN uRet

Posted: Mon Sep 11, 2006 8:10 pm
by Brian Hays
Terrific, Thanks!

Re: Generic ActiveX methods via OnError

Posted: Thu Jun 11, 2009 9:15 am
by Antonio Linares