Skip to main content

Methods

Methods

There are two kinds of methods: instance methods and class methods (called static methods in other languages). Instance methods are available only in object classes.

In InterSystems IRIS, methods can be written in ObjectScript or Python. To specify which language you will write a method in, use the following syntax:

Method MyMethod() [ Language = objectscript ]
{
    // implementation details written in ObjectScript
}
Method MyMethod() [ Language = python ]
{
    # implementation details written in Python
}

If a method does not use the Language keyword the compiler will assume that the method is written in default language for the class.

You must write the method’s language in all lowercase letters, as in the example.

References to Other Class Members

Within a method, use the syntax shown here to refer to other class members:

  • To refer to a parameter, use an expression like this:

     ..#PARAMETERNAME
    
    # technique 1
    iris.Package.Class._GetParameter("PARAMETERNAME")
    
    # technique 2
    objectinstance._GetParameter("PARAMETERNAME")
    

    In classes provided by InterSystems, all parameters are defined in all capitals, by convention, but your code is not required to do this.

  • To refer to another instance method, use an expression like this:

     ..methodname(arguments)
    
    self.methodname(arguments)
    

    Note that you cannot use this syntax within a class method to refer to an instance method.

  • To refer to another class method, use the following syntax:

     ..classmethodname(arguments)
    
    # technique 1
    iris.Package.Class.classmethodname(arguments)
    
    # technique 2
    iris.cls(__name__).classmethodname(arguments)
    

    Note that you cannot use the Python self syntax to access a class method. Instead, you can use the __name__ property to obtain the name of the current class, as shown in the above example.

  • (Within an instance method only) To refer to a property of the instance, use an expression like this:

     ..PropertyName
    
    self.PropertyName
    
    

    Similarly, to refer to a property of an object-valued property, use an expression like this:

     ..PropertyNameA.PropertyNameB
    
    self.PropertyNameA.PropertyNameB
    

    The syntax used in the ObjectScript examples is known as dot syntax.

    Also, you can invoke an instance method or class method of an object-valued property. For example:

     Do ..PropertyName.MyMethod()
    self.PropertyName.MyMethod()
    

References to Methods of Other Classes

Within a method (or within a routine), use the syntax shown here to refer to a method in some other class:

  • To invoke a class method and access its return value, use an expression like the following:

     ##class(Package.Class).MethodName(arguments)
    
    iris.Package.Class.MethodName(arguments)
    
    

    For example:

     set x = ##class(Util.Utils).GetToday()
    x = iris.Util.Utils.GetToday()
    

    You can also invoke a class method without accessing its return value as follows:

     do ##class(Util.Utils).DumpValues()
    iris.Util.Utils.DumpValues()
    
    Note:

    ##class is not case-sensitive.

  • To invoke an instance method, create an instance and then use an expression like the following in either ObjectScript or Python to invoke the method and access its return value:

    instance.MethodName(arguments)
    

    For example:

     Set x=instance.GetName()
    x=instance.GetName()
    

    You can also invoke an instance method without accessing its return value by calling the method as follows:

     Do instance.InsertItem("abc")
    instance.InsertItem("abc")
    

Not all methods have return values, so choose the syntax appropriate for your case.

References to Current Instance

Within an instance method, sometimes it is necessary to refer to the current instance itself, rather than to a property or method of the instance. For example, you might need to pass the current instance as an argument when invoking some other code.

In ObjectScript, use the special variable $THIS to refer to the current instance. In Python, use the variable self to refer to the current instance.

For example:

 Set sc=header.ProcessService($this)
sc=header.ProcessService(self)
FeedbackOpens in a new tab