Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Limitations of ArrayLists in VB.Net

Posted by Ravi Varma Thumati on September 22, 2009

Generics are new feature provided with version 2.0 of the Microsoft.Net framework. Generic classes and methods combine re-usability, type safety and efficiency in a way that there non-generics counterparts do not/cannot.

In this part some features of ArrayLists and there shortcomings/limitations will be discussed. The code in this article has been written in Visual Studio 2005 in VB.Net.

Earlier ArrayLists used to server the purpose, but that in a certain limit. Moreover, using ArrayLists to store different types came as a good performance cost. At the client end it shows our desired type that we are storing in the ArrayList but internally there’s much more that goes on.

Sample the code below:-

Dim myList As New System.Collections.ArrayList()

myList.Add(22)

myList.Add(“VB.Net Generics”)

myList.Add(22.45)

The usage convenience that is perceived above comes at a cost. Any reference or value type that is stored or added in the ArrayList (myList) is implicitly upcasted to System.Object type. And while retrieval, the reverse happens – downcasting to the appropriate type takes place.

Moreover, there is compromise on the Type Safety front also.  Consider the code below:-

Dim item As Integer = 0

 

‘This will cause an InvalidCastException

Dim x As Integer

For Each x In  myList

   Item = item + x

Next x

 

The reason is evident.

In .Net 1.x type-safety was achieved by writing your own typed ArrayList. But again, in that case re-usability was a major issue. Consider the following code:-

Imports System

Imports System.Collections

Imports System.Collections.Generic

Imports System.Text

 

Namespace ArrayListSample

 

Class Person

    Private Name As [String]

    Private Age As Int32

    Private Address As [String]

 

    Public Sub New()

    End Sub ‘New

 

    Public Sub New(ByVal Name As [String], ByVal Age As Int32, ByVal Address As [String])

        Name = Name

        Age = Age

        Address = Address

    End Sub ‘New

 

    Public Sub New(ByVal Name As [String], ByVal Age As Int32)

        Name = Name

        Age = Age

        Address = [String].Empty

    End Sub ‘New

 

    Public Sub New(ByVal Name As [String], ByVal Address As [String])

        Name = Name

        Age = 0

        Address = Address

    End Sub ‘New

 

    Public Sub New(ByVal Name As [String])

        Name = Name

        Age = 0

        Address = [String].Empty

    End Sub ‘New

End Class ‘Person

 

Class PeopleCollection

    Inherits System.Collections.IEnumerable

 

    Private arPeople As New ArrayList()

 

    Public Sub New()

    End Sub ‘New

 

    Public Sub AddPeople(ByVal p As Person)

        arPeople.Add(p)

    End Sub ‘AddPeople

 

    Public Sub ClearPeople()

        arPeople.Clear()

    End Sub ‘ClearPeople

 

    Private Count As Integer

 

    Public ReadOnly Property PeopleCount() As Integer

        Get

            Return Count

        End Get

    End Property

 

    Public Function GetEnumerator() As System.Collections.IEnumerator

        Return arPeople.GetEnumerator()

    End Function ‘GetEnumerator

End Class ‘PeopleCollection

 

Class Client

 

    Public Shared Sub Main()

 

        Dim myPeople As New PeopleCollection()

        myPeople.AddPeople(New Person(“Saurabh”, 24, “Gurgaon”))

        myPeople.AddPeople(New Person(“Manu”))

 

        Dim Person As Person

        For Each Person In myPeople

            Console.WriteLine(Person)

        Next Person

 

        Console.ReadLine()

    End Sub ‘Main

End Class ‘Client

End Namespace ‘ArrayListSample

 

The above code does achieve type safety but then we will have to write an almost identical custom collection for each type we wish to contain. Because:-

myPeople.AddPeople(New Car())

It would be a compile time error, since the code/approach above achieves type safety.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: