Title Image

Don Xml's Grok This

The home of Don Demsak
Welcome to Don Xml's Grok This Sign in | Help
in Search

This Blog

Syndication

Site Sponsors

DonXml's All Things Techie

Implementing IEnumerable(Of T) and IEnumerable in Visual Basic.Net

OK, this is one that gets me every time I switch back to Visual Basic from C#, and is one of the times that I find C# much easier to work with.  Even with all the XML Literal goodness in Visual Basic 9, these types of things make me want to just code in C#.

When you try to implement 2 different interfaces that have the same member name, but return 2 different types, the syntax in C# is sooooo much easier to read and work with.  In this case, it I'm writing my own Query Provider for a pet project I'm working on, and the generic Query class needs to implement both IEnumerable(Of T) and IEnumerable.  Both Interfaces require a function called GetEnumerator, but the generic version of the Interface return a type of IEnumerator(Of T), and the non-generic version returns IEnumerator.  Here's what it looks like in C#:

public IEnumerator<T> GetEnumerator()

{

    return ((IEnumerable<T>)this.provider.Execute(this.BLOCKED EXPRESSION.GetEnumerator();

}

 

IEnumerator IEnumerable.GetEnumerator()

{

    return ((IEnumerable)this.provider.Execute(this.BLOCKED EXPRESSION.GetEnumerator();

}

 Now, the same thing, but in Visual Basic:

Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator

    Return DirectCast(Me._Provider.Execute(Me._Expression), IEnumerable(Of T)).GetEnumerator

End Function

 

Private Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator

    Return DirectCast(Me._Provider.Execute(Me._Expression), IEnumerable).GetEnumerator

End Function

Do you see the difference?  In C# I can just declare the function name as IEnumerable.GetEnumerator, where in VB I have to use a different function name and then use the Implements keyword to specify the name of the interface and the name of the method is being implemented.  Because our code reads left to right, I read the method name first.  In C#, all the important information is all together, but in VB, I have to read beyond the method name to the Implements phrase to find out where this member maps to, and when you are typing this, it is even harder (Intellisense does help here).

Oh, and if you use either of the common C# to Visual Basic conversion methods, KamalPatel's site or (my favorite) Reflector both do not convert this correctly.  Here's their converted code

Private Function IEnumerable.GetEnumerator() As IEnumerator

    Return (CType(Me.Provider.Execute(Me.Expression), IEnumerable)).GetEnumerator()

End Function

Which of course will not compile.

Published Thursday, September 27, 2007 9:38 PM by donxml
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Jason Haley said:

September 28, 2007 8:01 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About donxml

I’m an independent consultant, specializing in .Net solutions architecture, based out of New Jersey who also doubles as an evangelist for XML, Domain Driven Design, enterprise architecture and .Net. I do not work for Microsoft, the W3C or any other big company that you may know of (at least not yet). I’ve been an indie for over ten years, and although I’ve been tempted a couple times to take a job with companies like Microsoft, I’ve haven’t found something better than my current situation. I work mostly with the large pharmaceuticals that are based here in New Jersey, and usually find myself on long term contracts. Definitely not the prototypical indie consultant, but it lets me dedicate time to my non-income generating activities like the developer community stuff, plus financing open source projects like XPathmania and MVP-XML. If you would like to talk to me about doing some contract work, just contact me via the contact page. My rates vary widely, depending on lots of different variables, but mostly distance from Jersey, and type of work. Plus, I’ve been known to donate some of my code for various projects.
Powered by Community Server, by Telligent Systems