Visual Basic .NET/Collections
VB.NET supports a number of data structures called Collections. Collection can be either zero or one based, depending on how they are declared. A collection is similar to an array in that it can store multiple data entries. However, most collections have advanced methods associated with them that distinguish them from Arrays. Some of these collections are:
An ArrayList is a dynamic array, the size increases and shrinks when needed and stores elements as generic Object's, this provides us with the ability to store objects with differing types.
Add method edit
To add objects to the ArrayList use the System.Collections.ArrayList.Add(Object) method The following is an example of adding String objects to an Arraylist.
Module Module1 Sub Main() Dim MyArrayList As System.Collections.ArrayList = New ArrayList() MyArrayList.add(New String("a")) MyArrayList.add(New String("b")) MyArrayList.add(New String("c")) MyArrayList.add(New String("d")) Console.WriteLine("Press enter to continue") Console.ReadLine() End Sub End Module
A queue is a first-in/first-out style collection. A queue has two unique methods used to enqueue and dequeue information from it. Using the System.Collections.Queue.Enqueue method, an object can be added to the end of the collection. Items can later be removed using the System.Collections.Queue.Dequeue method. This method will take the first object in the queue collection and then remove it from that collection.
A stack is a last-in/first-out style collection. A stack has three unique methods used to store and retrieve information from it. Using the System.Collections.Stack.Push method, an object can be added to the top of the stack, pushing the objects below it lower into the stack. Items can later be removed from the stack using the System.Collections.Stack.Pop method. This method will return the object at the top of the stack and remove it from the stack. Another method, System.Collections.Stack.Peek, is similar to the Pop method except it returns the object without removing it from the stack.
A SortedList collection is a collection of objects that are sorted both by index (similar to an array) and by key. A key works in a manner similar to that of an index; however, unlike an index, a key can be any object.
Generics provide data structures that work for a variety of data types. While the collections discussed above store items as objects, generic collections store the values in a more specific type such as String or Integer.
A List(Of Type) allows a set of values to be accessed by index or as a collection. You can therefore mix array and collection syntax on the same set:
Dim Weekdays As New List(Of String) Weekdays.Add("Monday") Weekdays.Add("Tuesday") Weekdays.Add("Wednesday") Weekdays.Add("Thursday") Weekdays.Add("Friday") Console.WriteLine("Accessing members by index...") For i As Integer = Weekdays.Count - 1 To 0 Step -1 Console.WriteLine(Weekdays(i)) Next i Console.WriteLine("Accessing members as a collection...") For Each s As String In Weekdays Console.WriteLine(s) Next s