Code/Tea/Etc...

Duncan Mackenzie has too much time on his hands

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.
Published Friday, March 19, 2004 11:18 PM by Duncanma
Filed under:

Comments

 

Anonymous said:

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.
March 20, 2004 1:57 AM
 

Ninputer said:

Is there any methods to implement "New Constrains" in Visual Basic 2005?
March 20, 2004 3:08 AM
 

Cory Smith said:

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.
March 20, 2004 7:58 AM
 

Paul Vick said:

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.
March 20, 2004 11:52 AM
 

DarthPedro said:

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++.
March 20, 2004 1:11 PM
 

Shteff said:

What is "T"?
March 22, 2004 1:25 AM
 

Duncan Mackenzie said:

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.
March 22, 2004 4:19 PM
 

AB said:

When i try to write this code i get error T not defined.
March 22, 2004 8:29 PM
 

Duncan Mackenzie said:

Just in case anyone wasn't clear.... this code is for Whidbey, not VB.NET 2003 or 2002...
March 22, 2004 8:32 PM
 

Michael Stevens said:

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?
March 23, 2004 7:21 AM
 

Duncan Mackenzie said:

Michael, yep this most definitely works with Option Strict On
March 23, 2004 10:57 AM
 

DarthPedro said:

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?
March 23, 2004 11:10 AM
 

Richard Murillo said:

"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.
March 23, 2004 1:29 PM
 

Mick George said:


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!

March 24, 2004 4:55 PM
 

Robert Imperio said:

Coool!!!! Thanks for the sample.
March 24, 2004 5:27 PM
 

M Sameer Haider said:

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
March 25, 2004 9:42 AM
 

Raghavendra said:

Not working on my system.
error: T is not defined
email: vr4sss@yahoo.com
March 26, 2004 2:40 AM
 

KenB said:

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?
March 26, 2004 6:05 AM
 

Mike Schinkel said:

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... :)
March 31, 2004 1:42 AM
 

Anonymous said:

"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>
March 31, 2004 2:03 PM
 

Anonymous said:

This isn't a tech support blog. If you want the answer, work for it. Sheez.
March 31, 2004 2:05 PM
 

Hoang Anh said:

Hello, I want to study of VB
April 4, 2004 5:02 AM
 

John said:

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????????????????
April 7, 2004 2:07 PM
 

Reinier said:

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!
April 18, 2004 7:56 AM
Anonymous comments are disabled

This Blog

Syndication

News

This blog has moved to my own VB site

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker