Defining and Using Properties
You can define properties in any object class. This topic provides an overview.
Kinds of Properties
An object class can include any mix of the following kinds of properties:
-
Literal properties, where the property holds a literal value.
-
Stream properties, where the property is a stream class that provides access to the global that holds the value. In this case, the value can be an extremely large amount of data (longer than the string length limit).
-
Object-valued properties, where the property is an object class.
-
Serial properties, where the property is a serial object class.
-
Collection properties, where the property is a specialized object that provides access to a collection of values, either an array or a list. The collection can consist of either literal values or of objects.
-
Relationships, where the property is a specialized object that refers to another object class and that adds a specific form of constraint to the two connected classes. These are supported only for persistent classes.
Defining Properties
To add a property to a class definition, add an element like one of the following to the class:
Property PropName as Classname;
Property PropName as Classname [ Keywords ] ;
Property PropName as Classname(PARAM1=value,PARAM2=value) [ Keywords ] ;
Property PropName as Classname(PARAM1=value,PARAM2=value) ;
Where:
-
PropName is the name of the property.
-
Classname is the class on which this property is based. If you omit this, Classname is assumed to be %StringOpens in a new tab, which has a default maximum length of 50 characters. Classname can be a data type class (resulting in a literal property), an object class, or a serial object class.
Also, instead of Classname, you can use the following syntaxes to create a list property or an array property, respectively:
list of Classname
array of Classname
-
Keywords represents any property keywords. See Compiler Keywords.
-
PARAM1, PARAM2, and so on are property parameters. The available parameter depend on the class used by the property.
Notice that the property parameters, if included, are enclosed in parentheses and precede any property keywords. The property keywords, if included, are enclosed in square brackets.
Assigning Values to Properties
To assign a value to a property, in the simplest cases you do the following:
-
Create or open an instance of the containing class, obtaining an OREF to that instance.
-
Use the SET command and dot syntax to assign a value as follows:
Set oref.PropertyName = value
Where oref is the OREF returned in the first step, PropertyName is the name of a property of this object, and value is a suitable value for this property:
-
If this is a literal property, value should evaluate to a literal value.
-
If the property is an object (including serial objects), then value should be an OREF that refers to the applicable object class.
-
The details are somewhat different for collection properties, relationships, and stream properties. These are all object classes that provide an API to access data. . The following shows an example in which we set the first list item of the FavoriteColors property of a Sample.Person object.
set oref.FavoriteColors.GetAt(1)="red"
For another example, the following writes data into a stream property:
Do person.Memo.Write("This is some text. ")
Do person.Memo.Write("This is some more text.")
Referring to Property Values
To refer to a property value, in the simplest cases you do the following:
-
Create or open an instance of the containing class, obtaining an OREF to that instance.
-
Use dot syntax to refer to a property:
Set newvariable = oref.PropertyName
Where oref is the OREF returned in the first step, PropertyName is the name of a property of this object.
The details are somewhat different for collection properties, relationships, and stream properties. These are all object classes that provide an API to access data. The following shows an example in which we obtain the first list item from the FavoriteColors property of a Sample.Person object.
Set newvariable=oref.FavoriteColors.GetAt(1)
Similarly, the following writes data from a stream property to the console:
Write person.Memo.Read()
Using Property Methods
Every property adds a set of generated methods to the class. These methods include propnameIsValid(), propnameLogicalToDisplay(), propnameDisplayToLogical(), propnameLogicalToOdbc(), propnameOdbcToLogical(), and others, where propname is the property name. For details and a list of the methods, see Using Property Methods.
InterSystems IRIS uses these methods internally, and you can call them directly as well. In each case, the argument is a property value. For example, suppose that Sample.Person had a property named Gender with logical values M and F (and display values Male and Female). You could display the logical and display values for this property for a given record, as follows;
MYNAMESPACE>set p=##class(Sample.Person).%OpenId(1)
MYNAMESPACE>w p.Gender
M
MYNAMESPACE>w ##class(Sample.Person).GenderLogicalToDisplay(p.Gender)
Male