Skip to main content

Objects

Objects

An object value refers to an instance of an in-memory object. You can assign an object reference (OREF) to any local variable:

  SET myperson = ##class(Sample.Person).%New()
  WRITE myperson

To refer to the methods and properties of an object instance, use dot syntax:

 SET myperson.Name = "El Vez"

To determine if a variable contains an object, use the $ISOBJECT function:

 SET str = "A string" 
 SET myperson = ##class(Sample.Person).%New()

 IF $ISOBJECT(myperson) {
     WRITE "myperson is an object.",!
 } ELSE {
     WRITE "myperson is not an object."
 }
 
 IF $ISOBJECT(str) {
      WRITE "str is an object."
 } ELSE {
     WRITE "str is not an object."
 }

You cannot assign an object value to a global. Doing so results in a runtime error.

Assigning an object value to a variable (or object property) has the side effect of incrementing the object’s internal reference count, as shown in the following example:

  SET x = ##class(Sample.Person).%New()
  WRITE x,!
  SET y = ##class(Sample.Person).%New()
  WRITE y,!
  SET z = ##class(Sample.Person).%New()
  WRITE z,!

When the number of references to an object reaches 0, the system automatically destroys the object (invoke its %OnClose() callback method and remove it from memory).

FeedbackOpens in a new tab