How to Create a Generic Type in vb in Vb.net

 you will have to follow this steps First, examine the following classes. Classes Obj and Gen perform exactly the same tasks, but Obj uses the Object class to enable any type to be stored in its field, while Gen uses generics:

‘ VB

Class Obj

Public V1 As Object

Public V2 As Object

Public Sub New(ByVal _V1 As Object, ByVal _V2 As Object) V1 = _V1

V2 = _V2

End Sub

End Class

Class Gen(Of T, U) Public V1 As T Public V2 As U

Public Sub New(ByVal _V1 As T, ByVal _V2 As U) V1 = _V1

V2 = _V2

End Sub

End Class

//  C#

class Obj

{

public Object t;

public Object u;

public Obj(Object _t, Object _u)

{

t = _t;

u = _u;

}

}

class Gen

{

public T t;

public U u;

public Gen(T _t, U _u)

{

t = _t;

u = _u;

}

}

As you can see, the Obj class has two members of type Object. The Gen class has two field members  of type T and U. The consuming  code determines  the types for T and U. Depending  on how the consuming  code uses the Gen class, T and U could be a string, an int, a custom class, or any combination  thereof.

There is a significant limitation to creating a generic class (without constraints, as dis- cussed in the section “How to Use Constraints,” later in this chapter): Generic code is valid only if it compiles for ever y possible  constructed instance  of the generic, whether an Int, a string, or any other class. Essentially, you are limited to the capabili- ties of the base Object class when writing generic code. Therefore, you could call the ToString or GetHashCode method within your class, but you could not use the + oper- ator or the > operator. These same restrictions  do not apply to the consuming  code because the consuming  code declares a specific type for the generic.

How to Consume a Generic Type

When you consume a generic type, you must specify the types for any generics used. Consider the following Console application code, which uses the Gen and Obj classes:

‘ VB

‘ Add two Strings using the Obj class

Dim oa As Obj = New Obj(“Hello, “, “World!”) Console.WriteLine(CType(oa.V1, String) + CType(oa.V2, String))

‘ Add two Strings using the Gen class

Dim ga As New Gen(Of String, String)(“Hello, “, “World!”) Console.WriteLine(ga.V1 + ga.V2)

‘ Add a Double and an Integer using the Obj class Dim ob As Obj = New Obj(10.125, 2005) Console.WriteLine(CType(ob.V1, Double) + CType(ob.V2, Integer))

‘ Add a Double and an Integer using the Gen class Dim gb As New Gen(Of Double, Integer)(10.125, 2005) Console.WriteLine(gb.V1 + gb.V2)

//  C#

// Add two strings using the Obj class Obj oa = new Obj(“Hello, “, “World!”); Console.WriteLine((string)oa.t + (string)oa.u);

// Add two strings using the Gen class

Gen ga = new Gen(“Hello, “, “World!”); Console.WriteLine(ga.t + ga.u);

// Add a double and an int using the Obj class Obj ob = new Obj(10.125, 2005); Console.WriteLine((double)ob.t + (int)ob.u);

// Add a double and an int using the Gen class

Gen gb = new Gen(10.125, 2005); Console.WriteLine(gb.t + gb.u);

If you run that code in a Console application, the Obj and Gen classes produce exactly the same results. However, the code that uses the Gen class actually works faster because it does not require boxing and unboxing  to and from the Object class. (Boxing and unboxing  are discussed  in the section “What Are Boxing and Unboxing?” later in this chapter.)  In addition, developers would have a much easier time using the Gen class. First, developers  would not have to cast manually from the Object class to the appropriate types. Second, type errors would be caught at compile time rather than at run time. To demonstrate that benefit, consider the following code, which contains an error (shown in bold):

‘ VB

‘ Add a Double and an Integer using the Gen class Dim gb As New Gen(Of Double, Integer)(10.125, 2005) Console.WriteLine(gb.V1 + gb.V2)

‘ Add a Double and an Integer using the Obj class Dim ob As Obj = New Obj(10.125, 2005) Console.WriteLine(CType(ob.V1, Integer) + CType(ob.V2, Integer))

//  C#

// Add a double and an int using the Gen class

Gen gc = new Gen(10.125, 2005); Console.WriteLine(gc.t + gc.u);

// Add a double and an int using the Obj class Obj oc = new Obj(10.125, 2005); Console.WriteLine((int)oc.t + (int)oc.u);

The last line in that code sample contains an error—the oc.V1 value (oc.t in C#) is cast to an int instead of to a double. Unfortunately,  the compiler won’t catch the mistake. Instead, in C#, a run-time exception is thrown  when the runtime  attempts  to cast a double to an int value. In Visual Basic, which allows narrowing conversions by default, the result is even worse—a miscalculation occurs. It’s much easier to fix a bug that the compiler catches and much harder  to detect and fix a run-time error, so the generic class provides a clear benefit.

About Author

Leave A Reply