AMT Help Files

Lists of Classes

It is possible to define lists of a Class type in LION using the normal syntax as shown in the example:

begin_definitions
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.

begin_definitions
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:

    loop for i := 1 to bikelist.count
        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:

    bikelist[1].bkcolor := 'Blue Metallic'
    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.
 In case of errors, ResOk and GetLastResult will be set to false.
 Optionally RESULTOKTO can be used with this function.

 See the description of the ToJson function for an example of a Json for the bike list as used in the page above.

 Example:

bikelist.FromJson (JsonAsString) RESULTOKTO ResokBoolVar
 FromXml (<XML String>)

 Adds elements to a list of classes from an XML String.
 In case of errors, ResOk and GetLastResult will be set to false.
 Optionally RESULTOKTO can be used with this function.

 The XML string must contain an ArrayOf<Class_name> element with elements of the class below it.
 See the description of the ToXml function for an example of an XML for the bike list as used in the page above.

 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.
 When no class instance in the list matches the class instance <Class>, 0 (zero) is returned.

 Optionally the index number from which element in the list to start the search can be set with the <StartFrom> parameter.
 If no 'StartFrom' parameter is given or a negative number is set as 'StartFrom', the search will start from the first element.
 If the 'StartFrom' index number is higher than the number of elements in the list, 0 (zero) will be returned.

 To be able to sort a list of classes, the class used in the list needs to comply with the following two requirements:

 
  • There must be a 'Compare' routine defined in the class. In this routine a three-way comparison must be written which will compare the class instances by one or more class members. For detailed information including examples, see Compare routine.
  • The class definition needs to have the keywords "Implements IComparer" set.
    FruitClass  : Class Implements IComparer
 Example:

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:

 
  • There must be a 'Compare' routine defined in the class. In this routine a three-way comparison must be written which will sort the class instances by one or more class members. For detailed information including examples, see Compare routine.
  • The class definition needs to have the keywords "Implements IComparer" set.
    FruitClass  : Class Implements IComparer
 Example:

FruitList.Sort ()
ToJson ()

 Returns all the elements in the list as a Json String.

 Note for structures in classes: While all the elements inside a structure will be included in the string, the structure itself will not be present in the string.

 The resulting Json string of the bike list used above on this page, will look as follows if written to a file:

 [
    {
       "make": "Ridley",
       "race": true,
       "bkcolor": "Carbon Black",
       "prodyear": 2012,
       "odometer": 13450
    },
    {
       "make": "DutchBike",

      ... etc

    }
 ]

 Example:

JsonAsString := bikelist.ToJson ()
ToXml ()

 Returns all the elements in the list as an Xml String.

 Note for structures in classes: While all the elements inside a structure will be included in the string, the structure itself will not be present in the string.

 The resulting XML string will have an ArrayOf<Class_Name> element with elements of the class below it.
 If written to a file (e.g. with FileControl Writefile), the bike list used above on this page will look as follows:

 <?xml version="1.0" encoding="utf-16"?>
 <ArrayOfbike xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <bike>
     <make>Ridley</make>
     <race>true</race>
     <bkcolor>Carbon Black</bkcolor>
     <prodyear>2012</prodyear>
     <odometer>13450</odometer>
   </bike>
   <bike>
     <make>DutchBike</make>

       .... etc

   </bike>
 </ArrayOfbike>

 Example:

XmlAsString := bikelist.ToXml ()

XML_FileControl.Writefile ('Bikelist.xml'0, XmlAsString, false)