Generic ActiveX methods via OnError

Post Reply
Brian Hays
Posts: 20
Joined: Fri Oct 14, 2005 7:56 am

Generic ActiveX methods via OnError

Post 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
-BH
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Post 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
Brian Hays
Posts: 20
Joined: Fri Oct 14, 2005 7:56 am

Post by Brian Hays »

Terrific, Thanks!
-BH
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Generic ActiveX methods via OnError

Post by Antonio Linares »

regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply