Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Related Posts
Laziness .NET -- the Professional Edition
Some new MSDN links of note
Visual Basic Resource Kit
Visual Basic Resource Kit Download Avail...
URLScan and the VB Resource Kit

Generic Methods...

Posted on March 20th
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.


Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Nice clear example!

Will have to find some useful places for this now...