PITADEV
Curiosity killed the developer's project.

Code Aphorism 2 - Ruined by VB

Thursday, 11 December 2008 19:41 by aj

William Wordsworth

I'm in the process of re-learning VB.Net.  I started with VB a long time ago, moved to C# with a career change, and now, due to another career change, am switching back.  I am unhappy about it, but am trying to keep an open mind.  Today was one of those days where my open mind slammed shut.

It was a situation similar to my first aphorism.  I had an array that I needed to get into a List<string>.  This time, however, the array was of an object provided as a return value from a method in a referenced API.  And the string I wanted to put in my List was a property of that return object, not that object itself.  Here's the long way:

Dim vendorAPI as new Vendor.API()
Dim things() as Vendor.API.Thing = vendorAPI.GiveMeThings()
List(Of String) stringsFromThings = new List(Of String)()
For i as Int32 = 0 to things.Length
   stringsFromThings.Add(things(i).StringField)
Next

I knew there had to be a cooler way to do this.  So I dinked around with it a bit, being rusty with VB and having never known these new tricks when I was writing in VB anyway, and couldn't figure it out.  With a little help from Kevin, I was able to Lambda-ize it in C#:

List<string> stringsFromThings = 
   things.ToList().ConvertAll(t => t.StringField);


Lovely, isn't it?  So I set about trying to do the same thing in VB, and so far this is the best I can come up with:

Dim stringsFromThings As List(Of String) = _
   things.ToList().ConvertAll(Of String)(Function(c _
      As Vendor.API.Thing) c.StringField)

After another day of cussing every time I tried to declare something as "Type varName" instead of the banner-of-VB "Dim varName as Type," I felt a little disgusted.  The main problem I have is that the VB compiler doesn't seem to have the ability to interpolate the type of "c," even though "c" has to be of type "Vendor.API.Thing."  And I'm still getting over the syntax thing, of course, but hopefully my open mind will open more.  If there's a better way, please tell me. I feel more like Wordsworth than I ever wanted to.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   Aphorisms | Rants
Actions:   E-mail | del.icio.us | Permalink | Comments (2) | Comment RSSRSS comment feed

Comments