Skip to main content

Working with List Collections

Working with List Collections

The examples in these sections show how to work with list properties, but you can use similar syntaxes to work with lists that are in a standalone collection.

Insert List Elements

To insert an element at the end of a list, use the Insert() method. For example, suppose that obj is a reference to an object, and Colors is a list property of the associated object. This property is defined as follows:

Property Colors as list of %String;

This code inserts three elements into the list. If the list was previously empty, then these elements are located at positions 1, 2, and 3, respectively.

 do obj.Colors.Insert("Red") // key = 1
 do obj.Colors.Insert("Green") // key = 2
 do obj.Colors.Insert("Blue") // key = 3

To insert an element at a specific position within a list, use the InsertAt() method. For example, this code inserts the string Yellow into the second position of the Colors property of obj.

 do obj.Colors.InsertAt("Yellow",2)

The list elements now have this order: "Red", "Yellow", "Green", "Blue". The new element is at position 2 and the elements previously at position 2 and 3 ("Green" and "Blue") move to positions 3 and 4 to make room for the new element.

The insertion of list elements works the same for objects. For example, suppose that pat is an object reference, and Diagnoses is a list property of the associated object. This property is defined as follows, where PatientDiagnosis is the name of a class:

Property Diagnoses as list of PatientDiagnosis;

This code creates a new class instance of PatientDiagnosis, stored in object reference patdiag, and inserts this object at the end of the Diagnoses list.

 Set patdiag = ##class(PatientDiagnosis).%New()
 Set patdiag.DiagnosisCode=code
 Set patdiag.DiagnosedBy=diagdoc
 Set status=pat.Diagnoses.Insert(patdiag)

Access List Elements

To access list elements, you can use these collection methods:

  • GetAt(key) – Return the element value at the position specified by key.

  • GetPrevious(key) – Return the element value at the position immediately before key.

  • GetNext(key) – Return the element value at the position immediately after key.

  • Find(value, key) – Starting after key, return the key of the next list element that equals value.

For example, this code iterates over a list and displays the elements of that list in order. The Count property of collections determines the number of elements to iterate over.

 set p = ##class(Sample.Person).%OpenId(1)
 for i = 1:1:p.FavoriteColors.Count() {write !, p.FavoriteColors.GetAt(i)}

Modify List Elements

To modify a value at a specified key, you can use the SetAt() method, as shown by this syntax:

 do oref.PropertyName.SetAt(value,key)

Here, oref is an object reference and PropertyName is the name of a list property of that object. For example, suppose that person.FavoriteColors is a list property of favorite colors containing elements red, blue, and green. This code changes the second color in the list to yellow:

 do person.FavoriteColors.SetAt("yellow",2)

Remove List Elements

To remove a list element, use the RemoveAt() method. For example, suppose that person.FavoriteColors is a list property of favorite colors containing elements red, blue, and green. This code removes the element at position 2 (blue)

 do person.FavoriteColors.RemoveAt(2)

The list elements now have this order: red, green. The element previously at position 3 (green) moves to position 2 to fill the gap caused by the removed element.

FeedbackOpens in a new tab