This is Part7 of QTP and DotNetFactory series by Saket. Refer Part1, Part2,Part3, Part4, Part5, Part6
Arraylist contains a list of values, we can add, insert, and remove very easily. We can add values without any size information which makes it a flexible data structure.
To work with this we just need to create an instance of ‘System.Collections.ArrayList’ using CreateInstance method.
Set MyList = DotnetFactory.CreateInstance("System.Collections.ArrayList")
Here are some of the functions which can be used to work with ArrayLists
Add – adds an item in an arraylist
MyList.Add("ListItem")
Remove – removes an item from the arraylist
MyList.Remove("ListItem ")
RemoveAt – removes an item from the specified position in the arraylist
MyList.RemoveAt(3)
Insert – Inserts an item at a specified position in the arraylist
MyList.Insert(3,"ListItem")
Sort – Sorts items in the arraylist
MyList.Sort
Let us now use these functions in QTP script. Consider a case where you need to sort a list in ascending or descending order.
First create the arraylist and add your list items in the arraylist. We will add five items in the list in random order.
Set MyList = DotnetFactory.CreateInstance("System.Collections.ArrayList") MyList.Add("ListItem5") MyList.Add("ListItem3") MyList.Add("ListItem2") MyList.Add("ListItem1") MyList.Add("ListItem4")
When you add a new item in the arraylist, it adds some empty memory locations, we should remove these empty memory locations. To do this we can use ‘TrimToSize’ method, which sets the capacity to the actual number of elements in the arraylist. You can easily understand this with the example below.
Example 26
Set MyList = DotnetFactory.CreateInstance("System.Collections.ArrayList") MyList.Add("ListItem5") MyList.Add("ListItem3") MyList.Add("ListItem2") MyList.Add("ListItem1") MyList.Add("ListItem4") Print "Before Trim" Print "ArrayList Capacity: " & MyList.Capacity Print "ArrayList Count: " & MyList.Count MyList.TrimToSize Print "After Trim" Print "ArrayList Capacity: " & MyList.Capacity Print "ArrayList Count: " & MyList.Count Print "View ArrayList Items" For nCnt = 0 To MyList.Count – 1 Print MyList.Item(CInt(nCnt) ) Next Set MyList = nothing
The properties ‘Capacity’ is used to get the number of elements that the arraylist can contain and ‘Count’ to get the total number of elements actually contained in the arraylist. Using a for loop you can easily go through the list items in the arraylist. Executing the above code outputs as –
You can sort the arraylist using ‘Sort’ method, which returns the list in ascending Sort. For descending sort method ‘Reverse’ can be used, but for this you will need to sort the list in ascending order using ‘Sort’ method first.
Putting it all together
Example 27
Set MyList = DotnetFactory.CreateInstance("System.Collections.ArrayList") MyList.Add("ListItem5") MyList.Add("ListItem3") MyList.Add("ListItem2") MyList.Add("ListItem1") MyList.Add("ListItem4") MyList.TrimToSize Print "View ArrayList Items" For nCnt = 0 To MyList.Count – 1 Print MyList.Item(CInt(nCnt) ) Next MyList.Sort Print "Sorted List - Asecending" For nCnt = 0 To MyList.Count – 1 Print MyList.Item(CInt(nCnt) ) Next MyList.Reverse Print "Sorted List - Descending" For nCnt = 0 To MyList.Count – 1 Print MyList.Item(CInt(nCnt) ) Next Set MyList = nothing
Returns –
You can search an item using ‘IndexOf’ and ‘BinarySearch’ method. These methods returns the zero-based index of the first occurrence of a value in the arraylist. To use BinarySearch you will need the items to be sorted in ascending order in arraylist.
Example 28
Set MyList = DotnetFactory.CreateInstance("System.Collections.ArrayList") MyList.Add("ListItem5") MyList.Add("ListItem3") MyList.Add("ListItem2") MyList.Add("ListItem1") MyList.Add("ListItem4") MyList.TrimToSize Print "View ArrayList Items" For nCnt = 0 To MyList.Count – 1 Print MyList.Item(CInt(nCnt) ) Next Print "Indexof- ListItem4 item found at: " & MyList.IndexOf("ListItem4") MyList.Sort For nCnt = 0 To MyList.Count – 1 Print MyList.Item(CInt(nCnt) ) Next Print "BinarySearch- ListItem2 item found at: " & MyList.BinarySearch("ListItem2") Set MyList = nothing
This will give you output as
Want to share your knowledge with the thousands of LearnQTP readers? Contact Us
Key: 26E2SJ9NT3H5