Skip to main content

Useful ObjectScript Functions

Useful ObjectScript Functions

ObjectScript provides the following functions for use with object classes:

  • $CLASSMETHOD enables you to run a class method, given as class name and method name. For example:

    TESTNAMESPACE>set class="Sample.Person"
     
    TESTNAMESPACE>set obj=$CLASSMETHOD(class,"%OpenId",1)
     
    TESTNAMESPACE>w obj.Name
    Van De Griek,Charlotte M.
    

    This function is useful when you need to write generic code that executes a class method, but the class name (or even the method name) is not known in advance. For example:

     //read name of class from imported document
     Set class=$list(headerElement,1) 
     // create header object
     Set headerObj=$classmethod(class,"%New")

    The other functions are useful in similar scenarios.

  • $METHOD enables you to run an instance method, given an instance and a method name. For example:

    TESTNAMESPACE>set obj=##class(Sample.Person).%OpenId(1)
     
    TESTNAMESPACE>do $METHOD(obj,"PrintPerson")
     
    Name: Van De Griek,Charlotte M.
    
  • $PROPERTY gets or sets the value of the given property for the given instance. For example:

    TESTNAMESPACE>set obj=##class(Sample.Person).%OpenId(2)
     
    TESTNAMESPACE>write $property(obj,"Name")
    Edison,Patrick J.
    
  • $PARAMETER gets the value of the given class parameter, given an instance. For example:

    TESTNAMESPACE>set obj=##class(Sample.Person).%OpenId(2)
     
    TESTNAMESPACE>write $parameter(obj,"EXTENTQUERYSPEC")
    Name,SSN,Home.City,Home.State
    
  • $CLASSNAME returns the class name for a given instance. For example:

    TESTNAMESPACE>set obj=##class(Sample.Person).%OpenId(1)
     
    TESTNAMESPACE>write $CLASSNAME(obj)
    Sample.Person
    

    With no argument, this function returns the class name of the current context. This can be useful in instance methods.

FeedbackOpens in a new tab