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.
[vbnet]
Public Class BatchExceptions
Implements ICollection(Of System.Exception)
Private mExceptionlist As ArrayList
.
.
.
End Class
[/vbnet]
That way, I could simply iterate over the list and perform the standard exception handling, like so:
[vbnet]
Dim exc As Exception
For Each exc In BatchExceptions
HandleError(exc)
Next
[/vbnet]
Of course, in order to make use of the “For Each” construct, I had to implement the GetEnumerator of the ICollection interface.
[vbnet]
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
[/vbnet]
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)’:
[vbnet]
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
[/vbnet]
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:
[vbnet]
private mExceptionlist as System.Collections.Generic.list
[/vbnet]
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 - How To Control Disk Thrash From ccmexec.exe (SMS Agent). Today started like any other day at work. I sat down, logged into my PC and was greeted by the thrashing sound of my hard drive. I've come to realize...
- 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...
- 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....
- Microsoft FxCop doesn't like Microsoft generated code! The other day I thought it might be nice to "do the right thing" and give my code a run against Microsoft's FxCop. I ran it right out of the...
- How to Delete Empty Folders - FREE! While performing a disk cleanup recently, I had cause to locate and delete any empty folders under a root folder. I knew there had to be a batch file command...
Related Websites - Israel eases Gaza import limits [/caption] JERUSALEM — Israel on Monday announced a major change in the way it will manage the country's controversial blockade of the Gaza Strip , a move Israeli officials hope will ease tensions with the Obama administration on the eve of a visit to Washington by Prime Minister Benjamin Netanyahu......
- How Lacquered Door The process of varnishing a door is to do a little maintenance at the door by creating a smoother surface, perfect and smooth while giving protection to the wood against water and other harmful agents. The process is long, but is not complicated, and I've made a video in Spanish......
- Kiss and Tell: Carmen Shirkey on Her Hero Today's article is written by romance author Carmen Shirkey. Okay, I decided to risk embarrassing Jim and participate in your new Kiss & Tell feature. :) My book, The List, is about a woman who has a ridiculously long list of characteristics she wants in her future boyfriend/husband. That was......
- List Builder Pro I just received this email from a trusted source. I know I am always looking for quality leads. This sounds worth checking into. If you're fed up with building your business one lead or subscriber at a time, then I've got some great news for you! But first ... Here's......
- EBay Versus Amazon.com Marketplace: Which One Is Easier And Better For Creating Extra Cash On The Internet There's a good possibility that you've paid for an item on ebay, or done shopping at Amazon's market place this year, however which one is the better of the two to Making Money?. It's dependent upon what you're selling!, ebay comes to mind as a good place to get rid......



