SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
ASP.NET, VB.NET, Microsoft .NET Framework, Microsoft Visual Studio, Windows Forms, Controls and more Tutorials and Articles for Programmers


ArrayList Samples in .NET Framework Applications


To create a new ArrayList you can define and create an instance of the ArrayList as follows

Dim arrList As New ArrayList()

This will initialize a new instance of System.Collections.ArrayList which is empty and has a default initial capacity.
You can also define an initial capacity while creating an ArrayList object using the below code

Dim arrList As New ArrayList(20)

The ArrayList class has Add method which is used the add Object type objects into the ArrayList instance.

arrList.Add(1)
arrList.Add(Sql.SqlDataSourceEnumerator.Instance)
arrList.Add("This is an ArrayList sample")

The above code statements adds different types of objects to the ArrayList using the Add method.

The ArrayList has Count readonly property which can be used to get the number of elements contained in the ArrayList object.
You can run the below code to see how you can use Count property after you dimension the arrList ArrayList object.

MessageBox.Show("ArrayList contains " & arrList.Count.ToString & " elements. ")

arrList.Count property will return the number of elements as an integer value.

To remove all elements from the container ArrayList you can use the Clear() method of the ArrayList class.

arrList.Clear()





You can set the capacity of an ArrayList object while creating the object. Or you can use the Capacity property to set or to get the capacity of the ArrayList any time.

If you did not define the Capacity property during the setup, while adding objects to the ArrayList, Capacity will be one more bigger than the element count in the ArrayList after the first Add method is called.
I mean, if you have added 5 elements then the Count property will be 5 and the Capacity property will be 6
If you call Clear() method just at that point, while the Count will be set to zero the Capacity will be unchanged as 6
If you continue adding elements, after the 6th element is added the Capacity will be doubled and you will see the new Capacity property is set to 12
So we can say that the Capacity of an ArrayList is doubled whenever the Capacity is full.


Here below you can see a sample code that adds integer values using Add method of the ArrayList class in a For loop
For i As Int16 = 1 To 30
      arrList.Add(i)
Next


You can sort or order elements within an ArrayList object using the Sort() method.
For example, if you populate an ArrayList with integer values using the above sample code, when you sort the ArrayList elements using

arrList.Sort()

The elements will be sorted as the mininum valued integer is placed into the first item and the biggest integer value is placed into the last item in the ArrayList object.
You can also re-order the ArrayList but this time in the descending way using Reverse() method as

arrList.Reverse()

But if you have objects within the ArrayList that can not be compared or comparable then you will get an exception like the one below:

System.InvalidOperationException was unhandled
Message="Failed to compare two elements in the array."
Source="mscorlib"
StackTrace:
at System.Array.SorterObjectArray.SwapIfGreaterWithItems(Int32 a, Int32 b) at System.Array.SorterObjectArray.QuickSort(Int32 left, Int32 right) at System.Array.Sort(Array keys, Array items, Int32 index, Int32 length, IComparer comparer) at System.Collections.ArrayList.Sort(Int32 index, Int32 count, IComparer comparer) at System.Collections.ArrayList.Sort()
InnerException: System.ArgumentException
Message="Object must be of type Int32."
Source="mscorlib"
StackTrace:
at System.Int32.CompareTo(Object value) at System.Collections.Comparer.Compare(Object a, Object b) at System.Array.SorterObjectArray.SwapIfGreaterWithItems(Int32 a, Int32 b)
InnerException:


You can also return or get the value of an element by passing the zero-based index of the item to the ArrayList

arrList(10)

This will get the 11th element in the ArrayList.
It is important that after you get the object contained in the ArrayList, you should cast the object to the proper type.
The below code adds a Button control to the ArrayList.
Then controls whether the Button1 object is exists in the ArrayList using the Contains() method. Contains() method return boolean true or false value according to the ArrayList object has the object or not.
Then the index of the object is found using IndexOf() method. IndexOf() method returns integer value indicating the index of an element within the ArrayList instance.

arrList.Add(Button1)

If arrList.Contains(Button1) Then
Dim btn As Button = CType(arrList(arrList.IndexOf(Button1)), Button)
MessageBox.Show(btn.Text)
End If

The last step is casting the object to the proper type and setting to a variable to get use of the methods and properties of the related object.


Visual Studio


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.