Tuesday, December 22, 2009

QTP and DotNetfactory – ArrayList

Sponsored Ad

Arraylist contains a list of values, you can add, insert and remove easily. You can add value with no information on the size which makes it a flexible information 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 “)

Insert - inserts an item at a specified position in the array list

MyList.Insert (3, "List Item")

RemoveAt - remove an item from the specified position in the array list

MyList.RemoveAt (3)

Sort - Sort items in the array list

MyList.Sort

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")

2.MyList.Add("ListItem5")

3.MyList.Add("ListItem3")

4.MyList.Add("ListItem2")

5.MyList.Add("ListItem1")

6.MyList.Add("ListItem4")

When you add a new item in the arraylist, it adds some empty memory locations, they ought to remove these empty memory locations. To do this they 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.

0 comments:

Post a Comment

Website Updates