Vishful thinking…

Switching from C# to VB.NET

Posted in VB.NET by viswaug on May 4, 2007

I am beginning to work with VB.NET for a new project and am going to the rough phase of transitioning to a new language. I have been working with c# for almost the past 5 years and the transition is definitely annoying. I am particularly not impressed with the verbosity of VB.NET when compared to C#. Anyway, in this post I just want to talk about an issue I was having when I was porting my code over from C# to VB.NET. I had created my own ReadOnlyDictionary<T, K> in C# so that I can set it as the datasource for a combobox. The Dictionary<T, K> that ships with .NET 2.0 is not quite ideal for the datasource property and setting the DisplayMember and the ValueMember of the combobox as the value and the key of the dictionary pair. So, when I was writing the same class in VB.NET, I had to implement both IEnumerable and IEnumerbale<T> in my class. Both these interface have a function named GetEnumerator() which only differ by their return type. The function in the IEnumerable interface returns a IEnumerator and the function in the IEnumerable<T> returns a IEnumerator<T>. When I implemented both these functions I ran into the error saying that I cannot declare two methods with the same name that only defer by the return type. In C#, since these methods are in different interfaces, I can still implement methods in my class by qualifying them with the name of the interface they belong to. But qualifying the method names with the name of the interface didn’t work in VB.NET. After, a few hours of banging my head against the wall I figured out that in VB.NET the name of the method does not need to match the name of the method in the interface. The only thing that is required is that I follow the definition of the method with an implements statement that specifies the name of the interface and the method that is being implemented. So, the solution in my case was defining the methods as below.

Public Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of TKey, TValue)) Implements IEnumerable(Of System.Collections.Generic.KeyValuePair(Of TKey, TValue)).GetEnumerator

Public Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator

Hopefully, that helps somebody that is going through the same problem.



2 Responses

Subscribe to comments with RSS.

  1. pons@vb gridview treeview said, on July 22, 2009 at 2:52 pm

    I hope you will love Visual Basic while developing. i too work in both the technologies side by side

  2. juni said, on September 21, 2011 at 10:34 am

    oh no, have been on C# for nearly 5years and now, am about to take a job using VB.NET … :S
    should i even be worried…? will things be really confusing? urgh!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: