Using Generic List in .NET

If you need to temporary store an array of values for later processing, you normally use and Array or a temporary table.
Array in Dynamics NAV is not dynamic and you cant search.
Temp table can be risky or has trigger code. But you can create a new table outside your license range, but again another object for a small task.

In comes .NET Generic List.
A simpel class for storing, retrieving and looping values.

Below is an examble that stores Item Numbers to a list, loops it, and finally queries the List if a specific Item Number exist in the List.

2017-04-24_1548

// Create instance of List
ItemList := ItemList.List();

// Loop and add data to List
Item.SETRANGE("Gross Weight", 10);
IF Item.FINDSET THEN BEGIN
 REPEAT
 // Find out if you want to store the Item No. 
 ItemList.Add(Item."No."); // Add Item No to the list
 UNTIL Item.NEXT = 0;
END;

// Loop the list (NAV2016)
FOREACH TextVariable IN ItemList DO BEGIN
 MESSAGE(TextVariable);
END;

// Loop the list (Pre NAV2016)
FOR i := 0 TO ItemList.Count() - 1 DO BEGIN
 MESSAGE(FORMAT(ItemList.Item(i)));
END;

// Check if Item No exist
IF ItemList.Contains('000001') THEN BEGIN
 MESSAGE('Item exist in list');
END;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s