Compartir a través de


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

        Debug.WriteLine(i)
        Debug.WriteLine(j)
        Debug.WriteLine("-------")

        Dim gm As New GenericMethodSample
        gm.Swap(Of Integer)(i, j)
        Debug.WriteLine(i)
        Debug.WriteLine(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

  • Anonymous
    March 19, 2004
    The comment has been removed
  • Anonymous
    March 19, 2004
    Is there any methods to implement "New Constrains" in Visual Basic 2005?
  • Anonymous
    March 19, 2004
    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.
  • Anonymous
    March 20, 2004
    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.
  • Anonymous
    March 20, 2004
    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++.
  • Anonymous
    March 21, 2004
    What is "T"?
  • Anonymous
    March 22, 2004
    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.
  • Anonymous
    March 22, 2004
    When i try to write this code i get error T not defined.
  • Anonymous
    March 22, 2004
    Just in case anyone wasn't clear.... this code is for Whidbey, not VB.NET 2003 or 2002...
  • Anonymous
    March 22, 2004
    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?
  • Anonymous
    March 23, 2004
    Michael, yep this most definitely works with Option Strict On
  • Anonymous
    March 23, 2004
    Michael,
    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?
  • Anonymous
    March 23, 2004
    The comment has been removed
  • Anonymous
    March 24, 2004

    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!

  • Anonymous
    March 24, 2004
    Coool!!!! Thanks for the sample.
  • Anonymous
    March 25, 2004
    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
    sameer_haider@ivcc.edu
  • Anonymous
    March 25, 2004
    Not working on my system.
    error: T is not defined
    email: vr4sss@yahoo.com
  • Anonymous
    March 25, 2004
    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?
  • Anonymous
    March 30, 2004
    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... :)
  • Anonymous
    March 31, 2004
    "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>
  • Anonymous
    March 31, 2004
    This isn't a tech support blog. If you want the answer, work for it. Sheez.
  • Anonymous
    April 03, 2004
    Hello, I want to study of VB
  • Anonymous
    April 07, 2004
    This code doesn't have the slightest resemblance of what I've been studying
    in jr.col. VB.net therefore, if this is the realworld, then what am i doing wasting my time and money in college for????????????????
  • Anonymous
    April 17, 2004
    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!