Arraylist and generics don’t mix with IEnumerable(Of T).GetEnumerator.
The other day I was writing an in-house tool to assist in some upgrades we were performing on client installations. This tool was supposed to perform its operations on a batch of items, and display the results upon completion.
Since processing this batch of items was a lengthy endeavor, I wanted the failure to process one of the items to simply be recorded and allow the processing of the others to continue. Part of the processing of each item was a call to multiple web services, so I would need a way to handle the collection of errors along the way and make them available for their eventual display.
I had what I thought was a clever idea: a private Arraylist of exceptions that occurred during processing.
Public Class BatchExceptions
Implements ICollection(Of System.Exception)
Private mExceptionlist As ArrayList
.
.
.
End Class
That way, I could simply iterate over the list and perform the standard exception handling, like so:
Dim exc As Exception
For Each exc In BatchExceptions
HandleError(exc)
Next
Of course, in order to make use of the “For Each” construct, I had to implement the GetEnumerator of the ICollection interface.
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of System.Exception) _
Implements System.Collections.Generic.IEnumerable(Of System.Exception).GetEnumerator
Return (mExceptionlist.GetEnumerator)
End Function
Cool. Only one problem:
That was OK though, because I was using generics after all. The compiler was being helpful and reminding me that I had to specify ‘IEnumerator(Of Exception)’:
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of System.Exception) _
Implements System.Collections.Generic.IEnumerable(Of System.Exception).GetEnumerator
Return (DirectCast(mExceptionlist.GetEnumerator, IEnumerator(Of Exception)))
End Function
Everything compiled fine, but at run-time I got the following RTE:
Unable to cast object of type ‘ArrayListEnumeratorSimple’ to type ‘System.Collections.Generic.IEnumerator`1[System.Exception]‘.
This was frustrating. It seemed like the compiler failed to warn me of this incompatibility, and simply kicked the can on down the road to the run-time to deal with.
Solution:
Well, I did a little poking around and finally ended up replacing my Arraylist with a list like so:
private mExceptionlist as System.Collections.Generic.list
Well, that did the trick but I can’t say it was as intuitive as it would seem. It makes sense, in hindsight, but why did I have to get some cryptic RTE? Why couldn’t the compiler have picked up on my use of an ArrayList and say, “Hey dummy - use a generic list!”? Still, I have a new trick to toss in my bag for the time I want to implement an enumerator on an Arraylist!
Related Posts - Lessons in Software Development From the Apollo Moon Missions. Monday was the 40th anniversary of the Apollo 11 lunar landing. The story of the Apollo program is an historic and inspirational one, but it's also relevant to software development....
- Annoying "next message" behavior in Thunderbird, and how to stop it! I love the Thunderbird email client. I use the portable version on my thumb drive, but one thing has always bugged the hell out of me when I use it:...
- Dude, Where's my Folder treeview?! After a wee bit O' overzealous registry cleaning last week, I had a minor panic. Well, a picture being worth a 1000 words, here's what I saw when I opened...
- Free Antivirus Recommendations (Protect Yourself Online With Free AntiVirus Software). I got tired of shelling out $40+ a year for antivirus protection long ago. Aside form the price, what really bugged me was the bloat! It became more and more...
- How to fake a TreeNodeCollection subclass in .NET If you've ever had reason to try to extend the standard Microsoft web TreeView control, you will have no doubt noticed that MS was quite unkind to you and sealed...
Related Websites - The Official Mommy-Muse.com™ List of What Not to Say to New Moms It should be obvious to anyone with a little common sense that one should not to say certain things to postpartum women. Oddly, these phrases still come out of people’s mouths, apparently bypassing their brains completely. For the benefit of people who care to be sensitive, as well as......
- Opening the Most Recently Used Document at Startup The Recent Documents list is great for quickly reopening files you’ve worked with recently, particularly because you can increase the size of the list, as described earlier. It’s not so great if other people have access to your computer because then they can easily see what you’ve been working on,......
- Sleep Better, Run Better [caption id="attachment_316" align="alignleft" width="180" caption="Run... run... run..."][/caption]If you're one of those exercises who plunges himself into a new habit with full force, you know what sore muscles feel like. There's a good chance you also know what it feels like to wake up in the morning, stiff, but still try......
- Solaris Tips and Tricks Find a list of Tips and Tricks here.A nice trick to list the process which has opened a particular port is : #!/bin/ksh## 7-30-2003# find from a port the pid that started the port#line='-------------------------------------------------------------------------'pids=`/usr/bin/ps -ef | sed 1d | awk '{print $2}'`# Prompt users or use 1st cmdline argumentif [......
- MonaVie and Inc. Magazine's 500 A lot of MonaVie distributors seem to tout the fact that Inc. magazine named MonaVie it's #1 choice in Food and Beverage and 18th overall. A few things should be noted about this list: It's a list of privately held companies - You won't see Coca-Cola, Pepsi, ConAgra foods or......



