Routines (alphabetical) > Routines: C > CALL_METHOD

# CALL_METHOD

The CALL_METHOD function or procedure calls an object method by name, passing any additional parameters as its arguments. CALL_METHOD is useful when you want to dynamically determine the method to call at run-time instead of compile-time.

Although not as flexible as EXECUTE, CALL_METHOD is much faster. Therefore, CALL_METHOD should be used in preference to EXECUTE whenever possible.

## Examples

###### Dynamic Method Calls on an Object

Here we dynamically either remove or add an object from a container, depending upon whether it is already contained. Instead of having two different method calls, we use CALL_METHOD with the appropriate method name.

obj = IDL_Container( )

obj.Add, IDL_Object( )

newobj = IDL_Object( )

CALL_METHOD, obj.IsContained(newobj) ? "Remove" : "Add", obj, newobj

print, obj.Count()

IDL prints:

2

###### Static Method Calls on a Variable

Next, we dynamically call a static method on a string variable:

var = "mY iDL stRing"

method = RANDOMU(seed) gt 0.5 ? "ToUpper" : "ToLower"

print, CALL_METHOD(method, var)

method = RANDOMU(seed) gt 0.5 ? "ToUpper" : "ToLower"

print, CALL_METHOD(method, var)

IDL prints (your result may vary!):

MY IDL STRING

my idl string

###### Static Class Method Calls

Finally, we call a static class method by just giving the name of the method:

print, CALL_METHOD("IDLUnit.ListUnits")

IDL prints:

meter gram second ampere kelvin mole candela bit micron inch point pica foot yard mile link...

In this last example you could also have called the method using an undefined variable with the appropriate name:

print, CALL_METHOD("ListUnits", IDLUnit)

## Syntax

Result = CALL_METHOD(Method, Item, Args..., Keywords=...)

or

CALL_METHOD, Method, Item, Args..., Keywords=...

## Return Value

If calling a function method, then the Result will be the result from that method.

## Arguments

### Method

A string containing the name of the method to be called. This argument can be a variable, which allows the called method to be determined at runtime.

You can also call a static class method by giving a method name of the form "Class.Method". In this case you should not supply the Item argument.

### Item

Set this argument to either a scalar object reference or an IDL variable of any type except STRUCTURE.

If Item is an object reference, then the Method will be called on that object, with Item being passed as the Self argument.

If Item is an IDL variable then the static method given by Method will be called on that variable. For example, if Item was a string and Method was "ToUpper", then the IDL_String.ToUpper method would be called on that string.

If Item is an undefined variable, then Item will be used as the name of a static class. For example, if Item was set to a variable named IDLUnit, and Method was "AddUnit", then the IDLUnit.AddUnit static class method would be called.

### Pi

The arguments to be passed to the method. These arguments are the positional and keyword arguments documented for the called method, and are passed to the called method exactly as if it had been called directly.

## Keywords

Any keywords are passed directly to the called method.

## Version History

5.1 | Introduced
8.4 | Added support for static method calls.

## See Also

CALL_FUNCTION, CALL_PROCEDURE, EXECUTE, Static Methods and Properties