How to: Define Type-Safe Collections (Visual Basic)

You can define and create a collection using the Collection class provided by Visual Basic, as the following example illustrates.

Public Class widgetRepository
    Public widgetColl As New Microsoft.VisualBasic.Collection()
    ' Insert code to implement additional functionality.
End Class

However, this widgetColl collection is not strongly typed. You can add any type of element to it, not just widget objects. When you retrieve an element, you might have to try to convert it to a widget. This can lead to type safety problems. For example, suppose you add a String to the collection using the following code.

Dim notWidget As String = "This is not a widget object!"

If you do this, any subsequent attempt to retrieve that element throws an ArgumentException exception at run time because the element of the collection is not of type widget.

We recommend that you maximize type safety by defining a generic class. This enforces strong typing and also provides flexibility in the particular data type it works with. For more information, see Generic Types in Visual Basic (Visual Basic).

To define a type-safe collection in a class

  • Use one of the generic classes in the System.Collections.Generic namespace, for example List<T>, for your collection class. Then you can create a collection that is restricted to widget members only. The following example illustrates how the declaration in the preceding example could be modified to create a generic collection.

    Public widgetColl As New System.Collections.Generic.List(Of widget)

    When you do this, the strong typing allows you to add only widget items to the collection, and every element you retrieve through the Item property is a widget object. Also because of this strong typing, the retrieving code can use all the properties and methods exposed by widget.


  • Create your own collection class without using any predefined classes. Restrict your Add method to accept only widget objects, and implement your Item property with a return type of widget. For more information, see How to: Define Type-Safe Collections (Visual Basic).

    If Item returned elements of type Object, you would have explicit access only to the properties and methods defined on the Object class. To access the widget members, you would have to either turn Option Strict Off or use the CType Function (Visual Basic) to explicitly convert the returned element to widget, as in the following code.

    Dim nextWidget As widget
        nextWidget = CType(widgetColl.Item(1), widget)
    Catch ex As Exception
        ' Insert code to run if the collection item is not a widget.
    End Try

    In either case, using Object elements in this way causes Visual Basic to employ late binding, which degrades your performance.

    Note that the Visual Basic Collection class accepts and returns Object elements, so it also has the disadvantages of weak typing and late binding.

Community Additions