Skip to main content

$ISOBJECT (ObjectScript)

Returns 1 or 0 based on whether an expression is an object reference (OREF).

Synopsis

$ISOBJECT(expr)

Argument

Argument Description
expr A ObjectScript expression.

Description

$ISOBJECT returns 1 if expr is an object reference (OREF). $ISOBJECT returns 0 if expr is not an object reference (OREF).

$ISOBJECT returns –1 if expr is a reference to an invalid object. Invalid objects should not occur in normal operations; an invalid object could be caused, for example, by recompiling the class while instances of the class are active.

To remove an object reference, set the variable to the null string (""). The obsolete %Close() method cannot be used to remove an object reference. %Close() performs no operation and always returns successful completion. Do not use %Close() when writing new code.

For information on OREFs, see OREF Basics.

Argument

expr

Any ObjectScript expression.

Examples

The following example shows the values returned by $ISOBJECT for an object reference and a non-object reference (in this case, a string reference):

  SET a="certainly not an object"
  SET o=##class(%SQL.Statement).%New()
  WRITE !,"non-object a: ",$ISOBJECT(a)
  WRITE !,"object ref o: ",$ISOBJECT(o)

The following example shows that JSON values are object references:

  SET a=["apple","banana","orange"]
  SET b={"fruit":"orange","color":"orange"}
  WRITE !,"JSON array: ",$ISOBJECT(a)
  WRITE !,"JSON object: ",$ISOBJECT(b)

The following Dynamic SQL example shows that a stream field is an OID, not an object reference. You need to use the SQL %OBJECT function to return the object reference:

  set myquery=2
  set myquery(1)="SELECT TOP 1 Name,Notes,%OBJECT(Notes) AS NoteObj "
  set myquery(2)="FROM Sample.Employee WHERE %OBJECT(Notes) IS NOT NULL"
  set tStatement = ##class(%SQL.Statement).%New()

  set qStatus = tStatement.%Prepare(.myquery)
  if $$$ISERR(qStatus) {write "%Prepare failed:" do $SYSTEM.Status.DisplayError(qStatus) quit}

  set rset = tStatement.%Execute()
  if (rset.%SQLCODE '= 0) {write "%Execute failed:", !, "SQLCODE ", rset.%SQLCODE, ": ", rset.%Message quit}

  while rset.%Next()
  {
    write "Stream field oid: ",$ISOBJECT(rset.Notes),!
    write "Stream field oref: ",$ISOBJECT(rset.NoteObj),!
  }
  if (rset.%SQLCODE < 0) {write "%Next failed:", !, "SQLCODE ", rset.%SQLCODE, ": ", rset.%Message quit}

The following example shows how to remove an object reference. The %Close() method does not change the object reference. Setting an object reference to the null string deletes the object reference:

  SET o=##class(%SQL.Statement).%New()
  WRITE !,"objref o: ",$ISOBJECT(o)
  DO o.%Close()  ; this is a no-op
  WRITE !,"objref o: ",$ISOBJECT(o)
  SET o=""
  WRITE !,"objref o: ",$ISOBJECT(o)

See Also

FeedbackOpens in a new tab