AMT Help Files

Type List

The type list is a list of elements of a certain type. Allowed are these LION types:


* A list of the type classes has different supported functionality than other list types. For information please read Lists of Classes.

 

The values inside a list can be read or changed by using the index number of the list element in brackets, e.g. <list>[<index>]
Assigning a new value to a list element is achieved with the same syntax: <list>[<index>] := <new value>
Besides reading and assigning new values of a list element, the element can be deleted, the list can be sorted, and the element with a given value can be found using the IndexOf function. See the table below for all the available functions.

When used in Forms and Reports the list will be cleared with every screen transaction and start of a Report. When used in the Global Definitions every part of the application which can use Global Definitions can alter the list and the changes will then be available to all the other parts of the application.

Type lists can be used as routine variables and as routine parameters. When used as routine parameters it can only be passed by reference, thus changes made to the list will also apply outside the routine and the VAR option is mandatory.

Using a list as the result of a routine is also possible, as shown in this example snippet:

routine WorkDays (VAR Employees: list (string), Weeknr: integer: list (string)
begin_routine
...
result.add ('Monday')
result.add ('Tuesday')
...
end_routine



Type lists may not be defined as a sub item of a structure and may not be used inside retained data.

The first item of a type list has index 1. When accessing a type list outside its bounds an exception is raised and the execution of the Form or Report will exit immediately.

Local Definitions:



<Name of List> : list (<Type> [<Length>[.Decimals]])

shoplist : list (alpha 20)


Using a list



A list can be maintained and used through a set functions shown below.

 Name   Description 
 Add (<Value>)  Adds an element with value in <Value> to the end of the list. <Value> must be of the type
 the list is defined in.

 Example:
 
shoplist.add ('potatoes')
 AddRange (<Sourcelist>)  Adds all the element of a list <Sourcelist> to the end of the list. Both lists must be of the same type, length and decimals.
 This function can be used to duplicate lists or to combine lists.  

 Example:
 
supplieslist.addrange (shoplist)
 Clear ()  Clears the contents of the list and sets the length of the list back to zero.

 Example:

shoplist.clear ()
 Count  Returns the current number of elements in the list.

 Example:

nr-elements := shoplist.count
 Delete (<Index>)  Deletes the element with index number <Index> from the list. The first element
 in the list index number 1.

 Example:

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

 Example:

shoplist.FromJson (JsonAsString) RESULTOKTO ResokBoolVar
 FromXml (<XML String>)  Adds elements to the list from an Xml String.
 In case of errors, ResOk and GetLastResult will be set to false.
 Optionally RESULTOKTO can be used with this function.

 Example:

shoplist.FromXml (XmlAsString)
 IndexOf (<Value>[, <StartFrom>])  Returns the index number of the element with value given in <Value>. When no element with <Value> is found, 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.
 
 Examples:
 
id-potatoes := shoplist.indexof('Potatoes')

id-2ndpotatoes := shoplist.indexof('Potatoes', id-potatoes)

id-apples := shoplist.indexof('Apples', 2)
 Insert (<Index>, <Value>)  Insert the value <Value> 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:

shoplist.insert (3'Apples')
 Sort ([ASC|DSC])  Sort the elements alphabetically. The sort can be either ascending (ASC) or descending
 (DSC). When no sort order is supplied it will default to ascending.

shoplist.sort (DSC)
 ToJson ()  Returns all the elements in the list as a Json String.

 Example:

JsonAsString := shoplist.ToJson ()
 ToXml ()  Returns all the elements in the list as an Xml String.

 Example:

XmlAsString := shoplist.ToXml ()


Code Example

// Add some elements
shoplist.add ('Potatoes')
shoplist.add ('Peanut butter')
shoplist.add ('Apples')
shoplist.add ('Bananas')
shoplist.add ('Cabbage')

sme ('Length of list should be 5 and is: ' + shoplist.count)

// show the list
sme ('Shopping list is:')
loop for i := 1 to shoplist.count
    sme (shoplist[i])
endloop

// Print first and second item
lay_1.firstitem     := shoplist[1]
lay_1.seconditem := shoplist[2]

// Delete the Peanut butter
pbutter          := shoplist.indexof ('Peanut butter')
shoplist.delete (pbutter)

// Add all the shoplist elements to an other list of the same type and length, then clear the list
supplieslist.addrange (shoplist)
shoplist.clear