Lists of Classes
It is possible to define lists of a Class type in LION using the normal syntax as shown in the example:
var
bikelist : list(bike)
The list can then be filled with items using the <list>.Add(<item>) command.
The list of Class items is in effect a list of assignments to class instances. That means that the Class item to add is an instance of the Class. This item needs therefore to be created using the class constructor <Class>.create() and filled with appropriate field values before it can be added to the list. |
var
bikelist : list(bike)
ibike : bike
end_definitions
routine main
begin_routine
ibike := bike.create()
ibike.make := 'Ridley'
ibike.race := true
ibike.bkcolor := 'Carbon Black'
ibike.prodyear := 2012
ibike.odometer := 13450
bikelist.add (ibike)
When (multiple) items have been added to the list, the individual items can be accessed assigning the item value (which is an assignment to an instance of the Class type) to a local variable of the Class type. For example:
ibike := bikelist[i]
disp_bike (ibike)
endloop
Where the routine call disp_bike(ibike) displays the bike fields.
It is very important to realize that the statement ibike := bikelist[i] only copies the assignment to the instance of the Class from the list to the Class variable. The effect is that when field members of the instance are changed using the Class variable ibike, these changes will be made in exactly the same instance as assigned to bikelist[i]. |
From AMT 94 onwards it is possible to access the members of the class instances inside the list directly by using the
index number of the list element in brackets, e.g. <list>[<index>].<member>
Example:
SME(bikelist[1].make + ' - ' + bikelist[1].bkcolor)
The following functions can be used with a list of classes:
Name | Description |
Add (<Class>) | Adds an element with a Class instance <Class> to the end of the list. <Class>
must be
an instance of the class definition the list is defined in. Example:
bikelist.add (ibike)
|
AddRange (<List>) | Adds all the elements/class instance assignments of a list <List> to the end of
the list. Both lists must be of the same (class)type. This function can be used to duplicate lists or to combine lists. Please note that only the assignments to the class instances will be copied, the instances itself will remain the same. Example:
inventorylist.addrange (bikelist)
|
Clear () | Clears the contents of the list and sets the length of the list back to zero. Example:
bikelist.clear ()
|
Count | Returns the current number of elements in the list. Example:
nr-elements := bikelist.count
|
Delete (<Index>) | Deletes the element with index number <Index> from the list. The first element in the list index number 1. Example:
bikelist.delete (3)
|
FromJson (<Json String>) |
Adds elements to the list from a Json String. See the description of the ToJson function
for an example of a Json for the bike list as used in the page above. bikelist.FromJson (JsonAsString) RESULTOKTO ResokBoolVar
|
FromXml (<XML String>) |
Adds elements to a list of classes from an XML String. The XML string must contain an ArrayOf<Class_name> element with elements of the class
below it. As example the XML String can be read from an XML file use the FileControl GiveFileContent fuction. Example: XmlAsString :=
XML_FileControl.GiveFileContent('Bikelist.xml', -1)
bikelist.FromXml (XmlAsString) |
IndexOf (<Class>[, <StartFrom>]) |
Returns the index number of the class instance which matches the class instance in
<Class> according to the compare class routine. To be able to sort a list of classes, the class used in the list needs to comply with the following two requirements:
FruitList.IndexOf (SearchFruit)
FruitList.IndexOf (SearchFruit, FruitIndex)
|
Insert (<Index>, <Class>) | Insert a Class instance <Class> in the list at place referenced by
<Index>. The element in that place and consecutive elements will shift one place down in the list. In this function the index may also be the length of the list plus one in which case the new element will be placed at the end of the list as with the Add function. Example:
bikelist.insert (3, ibike)
|
Sort () |
Sorts a list of classes according to the Compare routine of the class. To be able to sort a list of classes, the class used in the list needs to comply with the following two requirements:
FruitList.Sort ()
|
ToJson () |
Returns all the elements in the list as a Json String. The resulting Json string of the bike list used above on this page, will look as follows if written to a file: [ Example:
JsonAsString := bikelist.ToJson ()
|
ToXml () |
Returns all the elements in the list as an Xml String. The resulting XML string will have an ArrayOf<Class_Name> element with elements of the
class below it. <?xml version="1.0" encoding="utf-16"?> Example: XmlAsString := bikelist.ToXml ()
XML_FileControl.Writefile ('Bikelist.xml', 0, XmlAsString, false) |