Monday, July 9, 2018

Collection Classes "List"

A List object contains members that are accessed sequentially. Lists are
structures that can contain members of any X++ type. All the members in the
same list must be of the same type.
The following methods are commonly used on List objects:


addEnd(anytype) - adds a member to the end of the list.

addStart(anytype) - adds a member to the start of the list.

elements() - returns the number of members contained in the list.

getEnumerator() - returns a ListEnumerator object for this List
object.


typeId() - returns the Type that the List contains.

The
ListEnumerator class allows you to traverse through the elements within a

list. The following methods are commonly used on
ListEnumerator objects:

current() - retrieves the value of the item currently pointed to in the
list.


moveNext() - moves the enumerator to the next item in the list. List

enumerators start
before the first element in the list, so moveNext()
must be called to make it point to the first element in the list.


reset() - moves the enumerator to the start of the list.

The following example demonstrates how to create a list, then move through it
and extract values from it.
1. Create a new
List object, specifying the data type it will contain.
2. Add members to the List object, using the
List.addEnd() andList.addStart() methods.
3. Set the
ListEnumerator object using the List.getEnumerator()method.
4. Go to the start of the list, using
ListEnumerator.reset().
5. Move through the members of the list, using
ListEnumerator.moveNext().
6. Pull the value of the current member in the list, using
ListEnumerator.current().
 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
List integerList = new List(Types::Integer);
ListEnumerator enumerator;
// Add some elements to the list
integerList.addEnd(1);
integerList.addEnd(2);
integerList.addStart(3);
// Set the enumerator
enumerator = integerList.getEnumerator();
// Go to beginning of enumerator
enumerator.reset();
//Go to the first element in the List
enumerator.moveNext();
// Print contents of first and second elements
info(strfmt("First element is %1", enumerator.current()));
enumerator.moveNext();
info(strfmt("Second element is %1", enumerator.current()));

When the code is executed, the result in the infolog should be as follows:

First element is 3

Second element is 1

Best Regards,
Hossein Karimi  

No comments:

Post a Comment

Configure the Firewall on the Enterprise Portal Server

After you install Enterprise Portal, enable Web Server (HTTP) in Windows Firewall. If you do not enable the web server in Windows Firewall...