Can we take the VB syntax for generics one step further??

Duncan Mackenzie posted a little code sample that shows the sweet syntax of

For Each cust as Customer in Customers
 …
Next

which I so very much prefer over

Dim cust as Customer
for each cust in Customers

next

Simple, minor, but just a nice difference. I always hated that extraneous line of code. And hated it more when we moved to vb.net and could just declare a variable and give it a value all in one line.

However! …she said with a grin.

Why stop there?

Why do I even have to (re-)define what the objects in Customers are?

Why can’t I just write

for each whateverthehecktheobjectsareinmycollection in Customers
 …
next

and have .net look at that and say “well d’uh, I know what whateverthehecktheobjectsareinmycollection is going to be because Customers is filled with Customer objects, so I will just implicitly cast the whateverthehecktheobjectsareinmycollection to a Customer.

?

Just an idea… maybe it’s already there and I haven’t discovered it yet…

Or how about if I have a generic collection of generic objects?? Oh dear, it looks like I won’t be going to bed early tonight after all… Damn!

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.