How can I call a method where the method name is stored in a variable

148 views
Skip to first unread message

Brad Harum

unread,
May 1, 2013, 2:30:57 PM5/1/13
to Ensemble-in...@googlegroups.com
Hi,

I was trying to setup a function call that is dynamic where the name of the method I want to call is in a variable, like below where I am trying to execute "Function" variable which is set to "MyTestMethod", can someone please post or send me the syntax for this please..

{

  classmethod TestDynamicCall() As %Status
    {
      set Function = "MyTestMethod"
      w ..Function()
      Quit 1
    }

  classmethod MyTestMethod() As %String
    {
      Quit "It worked"
    }

}

Dale du Preez

unread,
May 1, 2013, 2:34:51 PM5/1/13
to Ensemble-in...@googlegroups.com
Hi Brad,

If you are on 2010.1 or newer, you can use the following syntax:

Classmethod TestDynamicCall() As %Status
{
      set Function = "MyTestMethod"
      write $classmethod($classname($this),Function)
      Quit 1
}

If you are on 2009.1 or older, you should be able to use the following syntax:

Classmethod TestDynamicCall() As %Status
{
      set Function = "MyTestMethod"
      write $zobjclassmethod(..%ClassName(1),Function)
      Quit 1
}

I hope that helps,
Dale
--
You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare Community" group.
To post to this group, send email to Ensemble-in...@googlegroups.com
To unsubscribe from this group, send email to Ensemble-in-Healt...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Ensemble-in-Healthcare?hl=en
---
You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare" group.
To unsubscribe from this group and stop receiving emails from it, send an email to Ensemble-in-Healt...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jonathan Levinson

unread,
May 1, 2013, 2:35:38 PM5/1/13
to Ensemble-in...@googlegroups.com

Instead of calling ..Function() call $CLASSMETHOD(classname, methodname, paramlist)

 

From the doc:

 

Executes a named class method in the designated class.

Synopsis

$CLASSMETHOD(classname, methodname, arg1, arg2, arg3, ... )

Parameters

classname

Optional — An expression that evaluates to a string. The content of the string must match exactly the name of an existing, accessible, previously compiled class. In the case of references to Caché classes, the name may be either in its canonical form (%Library.String), or its abbreviated form (%String).

If classname is omitted, the current class context is used. (You can use $THIS to determine the current class context.) Note that when classname is omitted the placeholder comma must be specified.

methodname

An expression which evaluates to a string. The value of the string must match the name of an existing class method in the class identified by classname.

arg1arg2,arg3, ...

Optional — A series of expressions to be substituted sequentially for the arguments to the designated method. The values of the expressions can be of any type. It is the responsibility of the implementor to make sure that the type of the supplied expressions match what the method expects, and have values within the bounds declared. (If the specified method expects no arguments then no arguments beyond the methodname need be given in the function invocation. If the method requires arguments, the rules that govern what must be supplied are those of the target method.)

Description

$CLASSMETHOD permits a Caché ObjectScript program to invoke an arbitrary class method in an arbitrary class. Both the class name and the method name may be computed at runtime or supplied as string constants. To invoke an instance method rather than a class method, use the $METHOD function.

If the method takes arguments, they are supplied by the list of arguments that follow the method name. A maximum of 255 argument values may be passed to the method.

The invocation of $CLASSMETHOD as a function or a procedure determines the invocation of the target method. You can invoke $CLASSMETHOD using the JOB command or the DO command, discarding the return value.

An attempt to invoke a nonexistent class results in a <CLASS DOES NOT EXIST> error, followed by the current namespace name and the specified class name. For example, attempting to invoke the nonexistent classname “Fred” results in the error <CLASS DOES NOT EXIST> *User.Fred. Specifying the empty string for classname results in <CLASS DOES NOT EXIST> *(No name).

An attempt to invoke a nonexistent class method results in a <METHOD DOES NOT EXIST> error.

Examples

--

tirthankar bachhar

unread,
May 1, 2013, 2:51:38 PM5/1/13
to Ensemble-in...@googlegroups.com
Another way is to use XECUTE. Though its not a standard solution. Using $CLASSMETHOD() is more eligant solution.
--
Tirthankar Bachhar
________________________________________________________
cell: +919331123037 | email: tirthanka...@gmail.com
--------------------------------------------------------------------------------------------------
Reply all
Reply to author
Forward
0 new messages