First Week Back at Work

This week was my first week back to work, and I already learned something new (that's why I love what I do...constantly learning)...that is, I learned it when I wasn't walking to and from work in the rain (5 minute walk, however it has rained here for about a week straight). The first project I've been slated to do is a small upgrade for an internal application (won't bore with details), basically a new report viewer has been written in managed code and needs to replace an existing ActiveX control hosted in IE. The current application is written in ASP, so of course the first step was to rewrite some of the pages in ASP.NET. Unfortunately, there were already a few pages that had been upgraded, and they written in VB.NET (nothing against VB, just prefer C# and am much more familiar with the syntax). That wasn't a big deal, until you introduce a few legacy COM servers they were using to generate the reports. One of the first things I like to do when rewriting existing code is write a prototype first that mimics the original functionality before refactoring into a more OO type solution. Of course, .Net makes interoperability with unmanaged COM objects a snap, however dealing with the return types of unmanaged functions can be tedious at best sometimes...as was the case in point.

The particular server in question got wrapped up with an RCW with return type of “object”, and upon further inspection, it actually returned an array of objects. No biggie, except in the legacy code these values were populated to an HTML table via manual looping through the return values (I really don't miss ASP) based on a parameter that specified a directory to retrieve the reports from. For example, if the user was an admin, it looped through both the parent directory and an admin directory to retrieve the reports to be displayed (hence two arrays). If the user was just a user, well...no sweat, just one array. Regardless, binding the array to a DataList is the solution I chose. So, it came down to how to combine two arrays together into a single array. Of course, an ArrayList is perfectly suited for this, however typecasting from an Array (object[] in this case) to an ArrayList is impossible. Also bear in mind that once an array's number of elements is defined/populated, this can't be changed easily without losing the contents of the original array. In the end it got a little messy as I struggled with losing the original array's values attempting to do Array.CopyTo || Array.Copy. In the end, it came down to one VB keyword that I didn't know existed...”Preserve” (when used in conjunction with ReDim). Yeah, I know there is a performance hit, but when you can't cast to the object you need, you are forced to act accordingly. In the end, the code I came up with was (bear in mind this is just harness code, “summit“ is the unmanaged COM server):

If Session("GroupID") = 1 Then ' hardcoded to = 1 for now

' get colparms

_colParmsAdmin = summit.GetReportCollection(_reportAdminDirectory)

End If

_colParms = summit.GetReportCollection(_reportDirectory)

ReDim Preserve _colParms(_colParms.Length + _colParmsAdmin.Length - 1)

_colParmsAdmin.CopyTo(_colParms, (_colParms.Length - _colParmsAdmin.Length))

' the following also works, but the above is easier to read

'Array.Copy(_colParmsAdmin, _colParmsAdmin.GetLowerBound(0), _colParms, _colParms.Length - _colParmsAdmin.Length, _colParmsAdmin.Length)

It's the little things that count (and are easiest to forget)...in this case, just using the Preserve keyword. Not to mention I haven't done much VB.NET coding. I'm just really used to C# syntax, plus this seems like a prime target for an overloaded operator (which of course VB.NET does not support). Some other gripes about VB.NET and arrays:

  • Why oh why oh why are arrays still one based in VB.NET??? Yes, VB is a more serious language than it used to be, but having to “-1“ determining the length of arrays just looks hacky. In this case, I was forced to “anti-debug“ the above codeblock until I got it right.
  • Marking arrays with parenthesis (as in Dim someObject() as Object). Actually, that's fine but it leads to readability issues down the line when you are assigning values to the elements...it looks like a method call with parameters. Of course, intellisense helps out...if you have VS.NET. Also, once you get used to seeing square brackets, you just really start to like those darn critters. I know MS chose this due to the legions of VB developers out there who are used to seeing this type of annotation, but if VB supported macros, that would be the first thing I would change.
  • This one is nitpicking: A declaration such as “Dim someObject() as Object” just doesn't lend itself to someObject being a type of System.Array...it looks like an array of someObject (which indeed it is), however the C# style of object[] someObject almost forces you to read it as “someObject is a type of object[]“, wherein object[] is a completely different type than object. Yeah, I'm just picky like that.

I won't write out the comparable C# code that accomplishes the same thing, it's pretty straightforward. All in all, interop is a pretty nifty feature, but dealing with non-managed return types can get a little finicky, especially when the source of the non-managed server is unavailable.

What else did I learn? That this is a good gig with a good group...I think I'll keep 'em around for a bit :-).

No comments yet