Generics C# and VB 2005

With some amusement I have read the recent threads on VB versus C#. My stand is quite simple, it’s the framework not the language. My personal favorite is C#, Pascal, VB, Simula, you name it. All except Simula (?) are available for .NET. I have to admit I like C# best but that is just a matter of taste, for the moment I have my reasons to focus on VB. VB 2005 has many new language features, more than C#. Working with generics I used some of these to write some “real C#-style” code in VB.


Generics are a nice way to work with typed collections. In a project I have a number of classes, all having the Car class as base class. Coding a generic collection in C#


Collection<Car> myCars;
myCars:= new Collection<Car>();


and in VB


Dim MyCars as Collection(of Car)
MyCars = new Collection(of Car)


There is more you can do with generics. Take a generic CarWash class which washes cars and maintains a collection of washed cars of a give subtype. This generic class has two type parameters, one for the type it will wash and one for the type it will list. Coding the class in C#:


    public class CarWash<CarToWash, CarToList>
        where CarToWash : Car
        where CarToList : Car
    {
        private Collection<CarToList> carList;


        public CarWash()
        {
            carList = new Collection<CarToList>();
        }


        public void Wash(CarToWash thisCar)
        {
            CarToList dirtyCar = thisCar as CarToList;
            if (dirtyCar != null)
            {
                carList.Add(dirtyCar);
            }
        }


    }


The class uses the CarToList type parameter to type the collection. It also uses the type parameter to test a car object against it with the as operator. As tries to convert an object to the type, when this fails the object is set to null. As is favorite operator of mine. Delphi has it as well.


But you can code the same in VB 2005 with the new TryCast function


Public Class CarWash(Of CarToWash As Car, CarToList As Car)


    Private TheList As Collection(Of CarToList)


    Public Sub New()
        TheList = New Collection(Of CarToList)
    End Sub


    Public Sub Wash(ByVal TheCar As CarToWash) 
        Dim DirtyCar as CarToList = TryCast(TheCar, CarToList)
        If DirtyCar IsNot Nothing Then
            TheList.Add(DirtyCar )
        End If
    End Sub

End Class


The TryCast function, new in VB 2005, behaves just like the as operator. Note also the new IsNot operator.


All cool stuff here is not in the languages but in the framework itself. If you want to bash VB first take a look at all the changes from VB 6 to VB.NET and the many new features in VB 2005. You have to C really sharp to spot the difference.


Peter

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Peter van 0oijen

    OK, point taken. That requires an intermediate var, Which is in my original code. I beleived this to be clearer.

    Ctype does not work on a var of a generic type. You have to use TryCast.

    That would result in this kind of code:

    if TypeOf(TheCar) is CarList then

    TheList.Add(TryCast(TheCar, CarToList))

    end if

    Which is not that clear either.

    But I will update the post. Perhaps it was to condensed.

  • Andrey Shchekin

    But why do you cast it twice ? The largest advantage of TryCast over TypeOf is a performance upgrade ("as" has the same advantage over "is" in C#).

    And if you do it this way, this advantage is nullified.