Setting an Index in a List
InstallShield 2020
InstallShield provides the ListSetIndex function, which lets you make an element the current element using an index number. If you know the location of a particular element in a list, you can call the ListSetIndex function to access that element immediately. You can traverse a list in either direction by using the index to set a specific element in a list to the current element. The index of the list starts at 0 (zero).
The ListSetIndex function works on both string and number lists. After you set the indexed element as the current element, call either the ListCurrentItem function or the ListCurrentString function to return the value of the indexed item.
This example demonstrates traversing a list non-incrementally using ListSetIndex.
listID = ListCreate (STRINGLIST);
GetGroupNameList (listID);
nCheck = ListSetIndex (listID, LISTFIRST);
while (nCheck != END_OF_LIST)
ListCurrentString (listID, svString);
MessageBox (svString, INFORMATION);
nCheck = ListSetIndex (listID, LISTNEXT);
endwhile;
ListDestroy (listID);
The ListCount function tells you how many elements are in a list. The ListCount function is used mainly for general information purposes, although it can be used to establish an upper index value in conjunction with ListSetIndex. For example, you can call ListCount to get the number of elements in a list, and use that value with ListSetIndex to traverse a list. The above example, which uses a while loop, is rewritten below using an InstallScript for loop based on the number of elements in the list.
listID = ListCreate (STRINGLIST);
GetGroupNameList (listID);
// Get the number of elements in the list.
nItems = ListCount (listID);
// Display the number of elements in the list.
SprintfBox (INFORMATION, "", "i = %d", nItems);
// Loop for nItems times beginning with zero,
// displaying each list element in turn in a message box.
for i = 0 to (nItems - 1)
ListSetIndex (listID, i);
ListCurrentString (listID, svString);
MessageBox (svString, INFORMATION);
nCheck = ListSetIndex (listID, LISTNEXT);
endfor;
ListDestroy (listID);
See Also