Microsoft.NET

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

Generics in VB.NET

Posted by Ravi Varma Thumati on September 22, 2009

In this article, I specifically talk about Generics in VB.NET and how they improve upon ArrayLists and how they solve the issues posed by ArrayLists.

In this article we shall focus on how these issues of type safety and reusability are very nicely handled by Generics. All with the help of code again

Imports System

Imports System.Collections.Generic

Imports System.Text

 

Namespace GenericsSample

 

Class Person

 

    Private _Age As Integer

 

    Public Property Age() As Integer

        Get

            Return _Age

        End Get

        Set(ByVal value As Integer)

            _Age = value

        End Set

    End Property

 

    Private _Name As [String]

 

    Public Property Name() As [String]

        Get

            Return _Name

        End Get

        Set(ByVal value As [String])

            _Name = value

        End Set

    End Property

 

    Private _Address As [String]

 

    Public Property Address() As [String]

        Get

            Return _Address

        End Get

        Set(ByVal value As [String])

            _Address = value

        End Set

    End Property

 

    Private _Company As [String]

 

    Public Property Company() As [String]

        Get

            Return _Company

        End Get

        Set(ByVal value As [String])

            _Company = value

        End Set

    End Property

 

    Public Sub New()

    End Sub ‘New

 

    Public Sub New(ByVal Name As [String])

        Me.Name = Name

        Me.Age = 0

        Me.Address = [String].Empty

        Me.Company = [String].Empty

    End Sub ‘New

 

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

        Me.Name = Name

        Me.Age = Age

        Me.Address = Address

    End Sub ‘New

End Class ‘Person

 

Class Program

 

    Overloads Shared Sub Main(ByVal args() As String)

        ‘Generic List Creation

        ‘List is a Generic Class provided by .Net Framework 2.0

        ‘System.Collections.Generics is the Namespace.

 

        List<Person> myPerson = new List<Person>();

        myPerson.Add(New Person(“Saurabh”))

        myPerson.Add(New Person(“Manu”))

        myPerson.Add(New Person(“SomeOne”, 24, “Gurgaon”))

        myPerson.Add(New Person(“SomeoneElse”, 24, “Gurgaon”))

 

        ‘myPerson.Add(new Car());

        ‘ This is A Compile Time Error

 

        Dim p As Person

        For Each p In myPerson

            Console.WriteLine(p.Name)

            Console.WriteLine(p.Age)

            Console.WriteLine(p.Address)

            Console.WriteLine(p.Company)

        Next p

 

        Console.ReadLine()

    End Sub ‘Main

End Class ‘Program

End Namespace ‘GenericsSample

 

Two classes can be seen in this code. Class Person is the class of which we want to create list for, and class Program is the main class where we actually create the list of persons and operate upon them.

How Generics tackle the issues posed by ArrayLists?

In the above code example Generic List class has been used to “contain” objects of type Person. At any time we can we can have the Generic List contain any other type, as below:-

‘List of Ints

 

List<int> myInts = new List<int>()

myInts.Add(5)

myInts.Add(10)

myInts.Add(20)

 

Dim x As Integer

For Each x In  myInts

   Console.WriteLine(x)

Next x

 

Console.ReadLine()

 

The above code snippet indicates that the same List class can be used to contain any datatype at any point of time, without requiring any kind of extra effort from the programmer’s side.

The syntax for using any kind of Generic Class is as under :-

GenericClass<T>  objT = new GenericClass<T>()

Where T is the datatype that’ want to list, and GenericClass is the Generic Class which will wrap our desired datatype (that’s the reason , “contains” ,above has been marked in the double quotes and is marked bold). This Generic Class can be our own custom Generic Class or the ones provided by the .Net Framework.

So technically, T gets replaced by the datatype at compile type. And that’s the reaosn why a compile time error occurs when castinig is not done properly, it will be an InvalidCast Exception while using ArrayLists. Thus Generics enforce type checking at complie time only, making life less difficult.

Performance is another area where Generics make it sweet when compared to ArrayLists. Since T is “replaced” by our datatype at comile time only so, no time and resources are wasted in boxing and unboxing the objects.

Thus Generics are a very powerful and nice feature provided with .Net 2.0. Generics types are found sprinkled throughout the .Net2.0 BCLs; however System.Collections.Generics namespace is chock full of them.

Example of a Generic Class

The following example shows a skeleton definition of a generic class.

Public Class classHolder(Of t)
    Public Sub processNewItem(ByVal newItem As t)
        Dim tempItem As t
        ' Insert code that processes an item of data type t.
    End Sub
End Class

In the preceding skeleton, t is a type parameter, that is, a placeholder for a data type that you supply when you declare the class. Elsewhere in your code, you can declare various versions of classHolder by supplying various data types for t. The following example shows two such declarations.

Public integerClass As New classHolder(Of Integer)
Friend stringClass As New classHolder(Of String)

The preceding statements declare constructed classes, in which a specific type replaces the type parameter. This replacement is propagated throughout the code within the constructed class. The following example shows what the processNewItem procedure looks like in integerClass.

Public Sub processNewItem(ByVal newItem As Integer)
    Dim tempItem As Integer
    ' Inserted code now processes an Integer item.
End Sub

Advantages of Generic Types

A generic type serves as a basis for declaring several different programming elements, each of which operates on a specific data type. The alternatives to a generic type are:

  1. A single type operating on the Object data type.
  2. A set of type-specific versions of the type, each version individually coded and operating on one specific data type such as String, Integer, or a user-defined type such as customer.

A generic type has the following advantages over these alternatives:

  • Type Safety. Generic types enforce compile-time type checking. Types based on Object accept any data type, and you must write code to check whether an input data type is acceptable. With generic types, the compiler can catch type mismatches before run time.
  • Performance. Generic types do not have to box and unbox data, because each one is specialized for one data type. Operations based on Object must box input data types to convert them to Object and unbox data destined for output. Boxing and unboxing reduce performance.

Types based on Object are also late-bound, which means that accessing their members requires extra code at run time. This also reduces performance.

  • Code Consolidation. The code in a generic type has to be defined only once. A set of type-specific versions of a type must replicate the same code in each version, with the only difference being the specific data type for that version. With generic types, the type-specific versions are all generated from the original generic type.
  • Code Reuse. Code that does not depend on a particular data type can be reused with various data types if it is generic. You can often reuse it even with a data type that you did not originally predict.
  • IDE Support. When you use a constructed type declared from a generic type, the integrated development environment (IDE) can give you more support while you are developing your code. For example, IntelliSenseâ„¢ can show you the type-specific options for an argument to a constructor or method.
  • Generic Algorithms. Abstract algorithms that are type-independent are good candidates for generic types. For example, a generic procedure that sorts items using the IComparable interface can be used with any data type that implements IComparable.

Constraints

Although the code in a generic type definition should be as type-independent as possible, you might need to require a certain capability of any data type supplied to your generic type. For example, if you want to compare two items for the purpose of sorting or collating, their data type must implement the IComparable interface. You can enforce this requirement by adding a constraint to the type parameter.

Example of a Constraint

The following example shows a skeleton definition of a class with a constraint that requires the type argument to implement IComparable.

Public Class itemManager(Of t As IComparable)
    ' Insert code that defines class members.
End Class

If subsequent code attempts to construct a class from itemManager supplying a type that does not implement IComparable, the compiler signals an error.

Types of Constraints

Your constraint can specify the following requirements in any combination:

  • The type argument must implement one or more interfaces
  • The type argument must be of the type of, or inherit from, at most one class
  • The type argument must expose a parameterless constructor accessible to the code that creates objects from it
  • The type argument must be a reference type, or it must be a value type

If you need to impose more than one requirement, you use a comma-separated constraint list inside braces ({ }). To require an accessible constructor, you include the New (Visual Basic) keyword in the list. To require a reference type, you include the Class (Visual Basic) keyword; to require a value type, you include the Structure (Visual Basic) keyword.

Example of Multiple Constraints

The following example shows a skeleton definition of a generic class with a constraint list on the type parameter. In the code that creates an instance of this class, the type argument must implement both the IComparable and IDisposable interfaces, be a reference type, and expose an accessible parameterless constructor.

Public Class thisClass(Of t As {IComparable, IDisposable, Class, New})
    ' Insert code that defines class members.
End Class

Important Terms

Generic types introduce and use the following terms:

  • Generic Type. A definition of a class, structure, interface, procedure, or delegate for which you supply at least one data type when you declare it.
  • Type Parameter. In a generic type definition, a placeholder for a data type you supply when you declare the type.
  • Type Argument. A specific data type that replaces a type parameter when you declare a constructed type from a generic type.
  • Constraint. A condition on a type parameter that restricts the type argument you can supply for it. A constraint can require that the type argument must implement a particular interface, be or inherit from a particular class, have an accessible parameterless constructor, or be a reference type or a value type. You can combine these constraints, but you can specify at most one class.
  • Constructed Type. A class, structure, interface, procedure, or delegate declared from a generic type by supplying type arguments for its type parameters.

 

Advertisements

2 Responses to “Generics in VB.NET”

  1. Bleaching said

    I just done mine and that i was looking for several design ideas and you gave me a few.

  2. I really enjoy reading through on this web site , it has excellent content .

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: