Visual Basic Generic Constraints

In visual basic, generics are useful to define a classstructure or methods with a placeholders (type parameters) to indicate that they can use any of the types.

 

Following is the example of defining generic class with type parameter (T) as placeholder with brackets (Of type).

 

Public Class GenericClass(Of T)

    Public msg As T

    Public Sub genericMethod(ByVal name As TByVal location As T)

        Console.WriteLine("{0}", msg)

        Console.WriteLine("Name: {0}", name)

        Console.WriteLine("Location: {0}", location)

    End Sub

End Class

If you observe the above class, we created a class (GenericClass) with one parameter (msg) and method (genericMethod) using type parameter (T) as a placeholder with brackets. Here the defined GenericClass doesn’t know anything about the defined placeholder and it will accept any type either string or int or class, etc. based on our requirements.

 

In case, if we want to restrict the generic class to accept only the particular type of placeholder, we need to use Constraints. By using Constraints, we can specify what type of placeholder the generic class can accept and the compiler will throw a compile-time error. In case, if we try to instantiate a generic class with the placeholder type that is not allowed by a constraint.

 

In visual basic, constraints are specified by using As keyword. Following table lists a different type of constraints available in visual basic.

 

ConstraintDescription
Of T As Structure The type argument must be a value type.
Of T As unmanaged The type of argument must not be a reference type.
Of T As Class The type argument must be a reference type.
Of T As New The type argument must have a public parameterless constructor.
Of T As <base class name> The type argument must be or derive from the specified base class.
Of T As <interface name> The type argument must be or implement the specified interface.
Of T As U The type argument supplied for T must be or derive from the argument supplied for U.

Visual Basic Generic Class with Constraints Example

Following is the example of defining a generic class with constraint using As contextual keyword in visual basic programming language.

 

Public Class GenericClass(Of T As Class)

    Public msg As T

    Public Sub genericMethod(ByVal name As T, ByVal location As T)

        Console.WriteLine("{0}", msg)

        Console.WriteLine("Name: {0}", name)

        Console.WriteLine("Location: {0}", location)

    End Sub

End Class

If you observe the above code, we defined a class (GenericClass) with (OfAs Class) constraint so the GenericClass will accept only the reference type arguments.

 

Following is the example of instantiating a generic class with valid reference type arguments such as string or class in visual basic.

 

' Instantiate Generic Class with Constraint

Dim gclass As GenericClass(Of String) = New GenericClass(Of String)()

Dim gclass1 As GenericClass(Of User) = New GenericClass(Of User)()

' Compile Time Error

' Dim gclass11 As GenericClass(Of Integer) = New GenericClass(Of Integer)()

If you observe the above code, we created an instance of GenericClass using reference type arguments such as string and class. In case, if we uncomment the commented code, we will get a compile-time error because int is a value type, not a reference type.

Visual Basic Multiple Generic Constraints

In visual basic, we can apply multiple constraints on generic classes based on our requirements. Following is the code snippet of adding multiple constraints to the generic class.

 

Public Class GenericClass(Of T As Class, X As Structure)

    ' Implementation

End Class

If you observe the above code snippet, we added multiple constraints on generic class (GenericClass) using As keyword.

Visual Basic Generic Methods with Constraints

In visual basic, we can also apply constraints on generic methods based on our requirements. Following is the example of adding constraints on a generic method using As keyword.

 

Public Class GenericClass(Of T As Class)

    Public msg As T

    Public Sub genericMethod(Of X As Class)(ByVal name As T, ByVal location As T)

        Console.WriteLine("{0}", msg)

        Console.WriteLine("Name: {0}", name)

        Console.WriteLine("Location: {0}", location)

    End Sub

End Class

Visual Basic Generic Constraints Overview

Following are the important points which we need to remember about generic constraints in visual basic programming language.

 

  • In visual basic, constraints are useful to restrict generics to accept only the particular type of placeholders.
  • By using As keyword, we can apply a constraints on generics.
  • In visual basic, we can apply multiple constraints on generic class or methods based on our requirements.
  • In visual basic, we have a different type of constraints available, those are classstructure, unmanaged, new(), etc.