Language > Objects > Using IDL Objects > Static Methods and Properties

# Static Methods and Properties

## Static Methods

In addition to calling methods on an object instance, you can also call static (or class) methods on a class. In order to call a method as "static," you will need to mark the method with a special "compile_opt static" flag. Usually, the documentation for the class will indicate which methods are object methods and which are static class methods.

### Static Procedure Methods

IDL static procedure methods have the syntax:

Classname.ProcedureName[, Arguments] [, KEYWORDS=Keywords]

where:

- Classname is a valid class name
- ProcedureName is the name of the static procedure method
- Arguments are the required or optional positional parameters
- Keywords are any optional keywords.

Note: The square brackets are not used in the actual call. They are simply used in the documentation to denote that the arguments and keywords are optional.

For example:

MyClass.DoSomething, a, b, /SPECIAL

This will execute the `DoSomething` class method on the `MyClass` class, passing in two arguments a and b, as well as a keyword set equal to 1.

### Static Function Methods

IDL static function methods have the syntax:

Result = Classname.FunctionName( [, Arguments] [, KEYWORDS=Keywords] )

where:

- Result is the returned value
- Classname is a valid class name
- FunctionName is the name of the static function method
- Arguments are the required or optional positional parameters
- Keywords are any optional keywords

Note: Again, the square brackets are not used in the actual call, they are simply used to denote that the arguments and keywords are optional.

For example:

Result = MyClass.Calculate(a, b, /ALL)

This statement executes the `Calculate` class method on the `MyClass` class, passing in two arguments a and b, as well as a keyword set equal to 1.

### Arguments

The Arguments section describes each valid argument to the method.

Note: These arguments are positional parameters that must be supplied in the order indicated by the method’s syntax.

### Named Variables

Often, arguments that contain values upon return from the function or procedure method (“output arguments”) are described as accepting “named variables.” A named variable is simply a valid IDL variable name. This variable does not need to be defined before being used as an output argument. Note, however that when an argument calls for a named variable, only a named variable can be used—sending an expression causes an error.

### Keywords

The Keywords section describes each valid keyword argument to the method.

Note: Keyword arguments are formal parameters that can be supplied in any order.

Supply keyword arguments to IDL methods by including the keyword name followed by an equal sign (“=”) and the value to which the keyword should be set.

Note: You can abbreviate keywords to their shortest unique length. For example, you can abbreviate the XSTYLE keyword to XST.

### Setting Keywords

When the documentation for a keyword states something similar to, “Set this keyword to enable logarithmic plotting,” the keyword is simply a switch that turns an option on and off. In general, setting such keywords equal to 1 (or using the /KEYWORD syntax) causes IDL to turn the option on. Explicitly setting the keyword to zero (or not including the keyword) turns the option off.

## Static Properties

Classes can also have static properties, which can be accessed using the "dot" syntax:

Value = Classname.PropertyName

For example:

value = MyClass.Color

PRINT, MyClass.Linestyle

Internally, IDL routes the class properties to the class' GetProperty method. Because static classes have no "state" and do not persist, you will need to hard-code any static property values within the GetProperty method, or you will need to compute them "on the fly."

## Static Method Calls versus Array Indexing

In IDL, by default both square brackets [ ] and parentheses ( ) can be used for array indexing. However, parentheses are also used for function calls. Because of this duplication, the IDL compiler cannot distinguish between function calls and array indexing. For example, you might have a static method call:

Result = MyClass.Calculate(a, b)

However, it is just as likely that you might be doing an array index into a structure:

Result = MyStruct.Field(a, b)

By default, IDL will be unable to distinguish between these two calls and in certain cases will throw a syntax error. To avoid any confusion, you should always use `compile_opt strictarr` (or `compile_opt idl2`) at the top of your procedures and functions. This will force you to use square brackets for all array indexing within that routine. The IDL compiler will then treat any parentheses as function calls.

### Use of Static Methods at the Main Program Level

Normally, if you are doing static method calls within a routine, it is straightforward to insert `compile_opt strictarr` to avoid any confusion with parentheses. However, if you are executing ad-hoc IDL commands at the main program level, it might be confusing to suddenly receive syntax errors when trying to make static method calls.

Because of this issue, if you use a static method call at the main program level (either at the command prompt or in a $MAIN program), IDL will then automatically turn on `compile_opt strictarr` for the main level. From that point on, you must then be sure to use square brackets for array indexing. See compile_opt for details.

## Examples of Static Methods

The Clipboard::Get and Clipboard::Set methods are static. For example, enter the following commands at the IDL prompt:

Clipboard.Set, "My text on the system clipboard"

print, Clipboard.Get()

IDL prints:

My text on the system clipboard

The IDLffVideoRead and IDLffVideoWrite classes have static methods for retrieving the list of available formats and codecs:

print, IDLffVideoRead.GetFormats()

print, IDLffVideoRead.GetCodecs()

print, IDLffVideoWrite.GetFormats()

print, IDLffVideoWrite.GetCodecs()

IDL prints:

avi flv gif mjpeg mov,mp4,m4a,3gp,3g2,mj2 swf wav

flv gif h263 h264 mjpeg mpeg4 ...

avi flv gif mjpeg mov mp4 swf wav webm

flv gif h263 mjpeg mpeg4 msmpeg4v2 msmpeg4 ...

The IDLUnit class has methods for adding, listing, and removing physical units:

; Add "shake" to the list of units.

IDLUnit.AddUnit, 'shake', '10 nanoseconds', PLURAL='shakes', SYMBOL='sh'

; Now that we have added the unit, we can use it:

PRINT, IDLUnit('2 shake to picoseconds')

IDL prints:

20000 picoseconds

## See Also

Creating Static Methods