Skip to main content

Introduction to Dot Syntax

Introduction to Dot Syntax

With an OREF, you can use dot syntax to refer to properties and methods of the associated object. This section introduces dot syntax, which is also discussed in later sections, along with alternative ways to refer to properties and methods of objects.

The general form of dot syntax is as follows:

oref.membername

For example, to specify the value of a property for an object, you can use a statement like this:

 Set oref.PropertyName = value

where oref is the OREF of the specific object, PropertyName is the name of the property that you want to set, and value is an ObjectScript expression that evaluates to the desired value. This could be a constant or could be a more complex expression.

We can use the same syntax to invoke methods of the object (instance methods). An instance method is invoked from a specific instance of a class and typically performs some action related to that instance. In the following example, we invoke the PrintPerson() method on the object whose Name property was just set:

 set person=##class(Sample.Person).%New() 
 set person.Name="Carter,Jacob N." 
 do person.PrintPerson()

If the method returns a value, you can use the SET command to assign the returned value to a variable:

SET myvar=oref.MethodName()

If the method does not return a value (or if you are uninterested in the return value), use either DO or JOB:

Do oref.MethodName()

If the method accepts arguments, specify them within the parentheses.

 Set value = oref.methodName(arglist)

Cascading Dot Syntax

Depending on the class definition, a property can be object-valued, meaning that its type is an object class. In such cases, you can use a chain of OREFs to refer to a property of the properties (or to a method of the property). This is known as cascading dot syntax. For example, the following syntax refers to the Street property of the HomeAddress property of a Person object:

 set person.HomeAddress.Street="15 Mulberry Street"

In this example, the person variable is an OREF, and the expression person.HomeAddress is also an OREF.

Note:

When referring to a class member generally, sometimes the following informal reference is used: PackageName.ClassName.Member, for example, the Accounting.Invoice.LineItem property. This form never appears in code.

Cascading Dot Syntax with a Null OREF

When you use a chain of OREFs to refer to a property, and an intermediate object has not been set, it is often desirable to return a null string for the expression instead of receiving an <INVALID OREF> error on the intermediate object. Thus if the intermediate object has not been set (is equal to a null string), the value returned for the chained property reference is a null string.

For example, if pers is a valid instance of Sample.Person and pers.Spouse equals "", then the following statement sets the name variable to "":

set name=pers.Spouse.Name

If this behavior is not appropriate in some context, then your code must explicitly check the intermediate object reference.

FeedbackOpens in a new tab