Generic Methods…

Someone suggested to me that VB.NET Whidbey didn’t have support for ‘Generic Methods’, so I quickly wrote a bit of sample code to check (yes, it does support Generic Methods) and I thought I’d post that test code for your amusement.

Public Class GenericMethodSample
Public Sub Swap(Of T)(ByRef i As T, ByRef j As T)
Dim temp As T
temp = j
j = i
i = temp
End Sub
End Class

Public Class Sample

Public Sub TestSwap()
Dim i, j As Integer
i = 3
j = 12


Dim gm As New GenericMethodSample
gm.Swap(Of Integer)(i, j)

End Sub

End Class

If you need the ‘blow-by-blow’ explanation of that code… the key lines to notice are;
    Public Sub Swap(Of T)(ByRef i As T, ByRef j As T)

Which declares a “Generic Method”, which is then strongly typed at runtime via code like this;
    gm.Swap(Of Integer)(i, j)

[Update]: Paul Vick points out that (Of Integer) can be skipped on the call, making it just

    gm.Swap(i, j)

because the compiler will infer the correct type argument.

Comments (24)

  1. Anonymous says:

    Lets kill some more VB6 users :-). Honestly, I can’t imaging anybody that can understand this type of stuff that wouldn’t use C# instead.

  2. Ninputer says:

    Is there any methods to implement "New Constrains" in Visual Basic 2005?

  3. Cory Smith says:

    I love it when anonymous people post such dribble.

    Excellent information Duncan, I was not aware of this being in the next VB. It looks like an excellent addition.

  4. Paul Vick says:

    Actually, the call can be simplified to gm.Swap(i, j) and the compiler will infer the correct type arguments. Also, we will support New constraints, although it is not in the PDC build (can’t vouch for later builds people may have).

    I guess I should probably write something on generics.

  5. DarthPedro says:

    Just because it’s allowable in the language, doesn’t mean that VB6 developers have to use it. If they’re more comfortable coding without generics that is still allowed. Shoot, there are lots of C++ programs that didn’t really get into templates either (unless they had to).

    And, if generics weren’t supported in Whidbey VB, then there would be developers saying that VB is not supporting the full set of .NET functionality. That it would be a second-class citizen, like some people consider VB6 in relation to C++.

  6. Shteff says:

    What is "T"?

  7. T is a placeholder… it is replaced with whatever type (Integer, String,Customer, etc…) you supply when you call the Method or create the Class.

  8. AB says:

    When i try to write this code i get error T not defined.

  9. Just in case anyone wasn’t clear…. this code is for Whidbey, not VB.NET 2003 or 2002…

  10. Michael Stevens says:

    Okay, I was feeling behind the pack till I read that generic methods are for Whidbey. I have never heard of them. They seem neat from looking at them but I can’t think of a reason why I’d use them. Having said that, I would ask if the generic method is supported with option strict on?

  11. Michael, yep this most definitely works with Option Strict On

  12. DarthPedro says:


    Basically, it’s useful when you want to create generic methods or classes, but still want to maintain strong typing.

    For example, the way collections currently work, they use the base type object to pass things in and out. You then have to cast it to the type you want. However, there’s nothing preventing someone from putting the wrong type into that collection, and causing bad things to happen when you make assumption about what’s in the collection.

    You can create typed collections that derive from CollectionBase that only accepts and returns classes of a particular type. However, you have to create these typed collections for every type you care about have a collection for.

    Generics lets you create typed collections easily. You define one collection class (like GenericArrayList<T> — sorry for the C# notation) and implement it based using T rather than any specific type. Then, when you create an instance of that class, you specify what type you want it to be (GenericArrayList<myType>) without having to re-implement all of the collection methods like you would need to do if you implemented it based on CollectionBase or ArrayList or which one you used…

    Does that make sense?

  13. "Lets kill some more VB6 users :-). Honestly, I can’t imaging anybody that can understand this type of stuff that wouldn’t use C# instead. "

    Coming from a background of C++ and then moving over to VB in the mid 90’s, VB has grown on me. I love VB.Net and am excited with the new release. I feel that there is not enough support for VB.Net. Yea, C# is more mainstream, but VB.Net…. c’mon.

  14. Mick George says:

    After developing in VB and VBA professionally for the last 5 years I am now developing in VB.NET (3 months) and I have to say that I am more than a little excited with what I find, every day turns up something new and exciting for me, programming is fun again!

  15. Robert Imperio says:

    Coool!!!! Thanks for the sample.

  16. M Sameer Haider says:

    I am a .Net Instructor (IVCC) and a programmer.How can I link a .pdf file at the end of my report?

    My email address is

  17. Raghavendra says:

    Not working on my system.

    error: T is not defined


  18. KenB says:

    Using such a technique, is this early bound or late bound? If you used a class type instead of a scalar type would you get all of the intellisense?

  19. Generics are awesome! Three cheers to the VB.NET team for including. (Of course why they couldn’t keep from overloading parens yet one more time is beyond me… 🙂

  20. Anonymous says:

    "Honestly, I can’t imaging anybody that can understand this type of stuff that wouldn’t use C# instead." — Just those of us stuck consulting who can’t persuade the client to go to C# <g>

  21. Anonymous says:

    This isn’t a tech support blog. If you want the answer, work for it. Sheez.

  22. Hoang Anh says:

    Hello, I want to study of VB

  23. John says:

    This code doesn’t have the slightest resemblance of what I’ve been studying

    in jr.col. therefore, if this is the realworld, then what am i doing wasting my time and money in college for????????????????

  24. Reinier says:

    Generic methods were discussed in college when *I* was there, 20 years ago, under the name of parametrized types, and everybody agreed that they’re very useful for the exact same reason that C++ templates are useful. Almost always when I create a Collection, I can say exactly which type/class the things in my collection are, but I can’t tell the compiler and let it check the type at compile time. With this feature, I can – and I will!