VB.NET v2.0: Generics are Supported in VB Also

Rob Chartier has created a really nice article titled Introducing .NET Generics.  Unfortunately, all of the sample code was written using C# -- just like every sneak peek on generics so far.  However, VB.NET will also fully support generics in .NET v2.0 (Whidbey), although not much has been stated about this.  Take a look at my VB.NET translations here -- you'll note that Of is the VB keyword used for generics, instead of using the <> syntax of C#. 

Given Rob's C# first generic type:

public class Col<T> {
  T t;
  public T Val {
    
get {
      return t;
    }
    
set {
      t =
value;
    }
  }

}

Here's the equivalent VB generic type:

Public Class Col(Of T)
  Dim _t As T
  Public Property Val() As T
    Get
      Return _t
    End Get
    Set(ByVal value As T)
      _t = value
    End Set
  End Property
End Class

Given Rob's C# first generic usage:

Col<string> mystring = new Col<string>();

Here's the equivalent VB generic usage:

Dim mystring As New Col(Of String)

Rob has a C# generic method also:

public static T[] CreateArray<T>(int size) {
  return new T[size];
}

And here's the equivalent VB again:

Public Shared Function CreateArray(Of T)(ByVal size As Integer) As T()
  Dim _t(size) As T
  Return _t
End Function

It looks like VB.NET also allows constraints on generics, although I haven't tested this yet.  I personally prefer C#, but I find many potential customers are interested in VB.NET, including my current employer.  So far C# is getting the headlines in v2.0, but it looks like VB.NET is doing just fine too.  By the way, the System.Collections.Generics namespace is full of pre-built generic collections, like Dictionary, List, Queue, Stack, and many others too.

Published Monday, October 27, 2003 8:07 PM by PaulWilson

Comments

# re: VB.NET v2.0: Generics are Supported in VB Also

Noooooo! They also used '()' for this one!!! Now 'foo()' can mean:
- array index
- generic type specification
- method call

Why oh why didn't they simply use a different set of brackets... This is cludgy... Ah well...

Tuesday, October 28, 2003 3:36 AM by Frans Bouma

# re: VB.NET v2.0: Generics are Supported in VB Also

Or drop the parenthesis altogether. I know it's code, not English, but putting a prepositional phrase in a parenthetical phrase just looks awkward.

Tuesday, October 28, 2003 12:15 PM by Richard Tallent

# re: VB.NET v2.0: Generics are Supported in VB Also

FWIW, I like the syntax. It looks like VB.

After they did array initializers with curly braces and used C syntax for bit shifting operators, I was getting worried.

Tuesday, October 28, 2003 10:47 PM by Dave Rothgery

# re: VB.NET v2.0: Generics are Supported in VB Also

Hey Rob.
I'm stuck. How do I convert the scottgu snippet to vb? eg:


public class OrderSystem
{
public List<Order> GetOrders() {

List<Order> orders = new List<Order>();

orders.Add(new Order(123, "Dell"));
orders.Add(new Order(345, "Toshiba"));
orders.Add(new Order(567, "Compaq"));

return orders;
}
}

Any ideas?

Tuesday, April 13, 2004 10:39 AM by Damian Barrow

# re: VB.NET v2.0: Generics are Supported in VB Also

Well, I'm not Rob, but . . .

Public Class OrderSystem
Public Function GetOrders() As List(Of Order)

Dim _orders As New List(Of Order)

_orders.Add(new Order(123, "Dell"))
_orders.Add(new Order(345, "Toshiba"))
_orders.Add(new Order(567, "Compaq"))

Return _orders
End Function
End Class

Tuesday, April 13, 2004 11:00 AM by Paul Wilson

# re: VB.NET v2.0: Generics are Supported in VB Also

I think the syntax is just fine.

Thursday, May 06, 2004 8:31 AM by Michael Ferrini

Leave a Comment

(required) 
(required) 
(optional)
(required)