In Visual Basic, there are several ways to organize data sets:
Standard arrays (Dim arr(10) As Integer): static size, fast access by index, cannot be dynamically resized.
Collection (object collection): dynamically resizable, can store elements of any type (but without strict typing), access by key and index, supports For Each.
ArrayList (in VB.NET): deprecated collection, stores objects of type Object, does not support generics (Generic), requires type casting.
List(Of T) (in VB.NET): typed collection, dynamic size, fast access, supports LINQ, type-safe.
Usage examples:
Standard array:
Dim numbers(4) As Integer numbers(0) = 10
Collection:
Dim coll As New Collection() coll.Add("apple") coll.Add(123) For Each item In coll Debug.Print(item) Next
ArrayList:
Dim arrList As New ArrayList() arrList.Add("abc") arrList.Add(123)
List(Of T):
Dim list As New List(Of Integer)() list.Add(10) list.Add(20) For Each num As Integer In list Console.WriteLine(num) Next
What is the main problem that can arise when working with ArrayList compared to List(Of T)?
Answer: ArrayList stores elements as Object, so explicit type casting is required when accessing elements, which increases the chance of getting an InvalidCastException at runtime. List(Of T) provides type safety at compile time, eliminating such errors.
Dim arrList As New ArrayList() arrList.Add(100) ' Dim s As String = arrList(0) ' Runtime error Dim n As Integer = CType(arrList(0), Integer) ' OK
Story
In the financial system, standard arrays were actively used to store dynamic-size data. When exceeding the pre-allocated size, the arrays "trimmed," resulting in the loss of transactions during peak loads.
Story
When working with ArrayList, strings and numbers were added to the collection, and then an attempt was made to calculate the sum of the elements. As a result, the application crashed with an exception during type casting, as strings cannot be summed with numbers.
Story
Used Collection to store order classes, but did not check the types of elements. By mistake, text was added instead of an order object in one module, which led to the failure of the entire business logic when trying to call methods of a non-existent object.