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 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...
- Google Chrome: the OS. Google announced last Tuesday that it has its sights on dethroning Microsoft as desktop OS king: "The new operating system, announced late Tuesday night on Google's Web site, will be...
- 10 Useful, Often Overlooked HTML Tags. Pop quiz: When would you use the <wbr> tag, and what does it do? Yeah, I had no idea either. In fact, I had never even seen this tag before,...
- Free Firewall Recommendations (Protect Yourself Online With These Free Firewalls) If your home computer is connected to the internet, or to other computers that are connected to the internet - you need a firewall. It's as simple as that. Hackers...
- 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 - Grocery Store Savings Tips Few spending categories can kill a budget faster than food. For some families the art of frugal grocery shopping comes naturally. We have to work a little harder, but over time we have developed a few tips to help navigate the grocery aisles. Sign up for customer loyalty programs. Larger......
- Tips for Organizing the Garage 1 - Begin by creating a staging area. If you can park at least one car in your two car garage, then you can probably organize everything else in a day. Create a staging area where you can put items as you take them out of the garage, such as......
- Find Out More About Internet Marketing - Now Anyone To Make Money In Their Own Home Business! Internet Marketing has made it possible for more and more people to finally start a business they can run from the privacy of their home. It doesn't cost a lot to get started and you do not need to hire employees to help you; at least not in the beginning.......
- plz. help me to write the main idea of this article in the NY Times. in two pages.? November 5, 2006 Where Plan A Left Ahmad Chalabi By DEXTER FILKINS 1. London, August 2006 Many miles away in a more dangerous place the dream is ending badly. The bodies pile up. Good people stream to the borders. Leaders pile money onto planes. The center is giving way. The......
- Making money with an eBay Store, Part 7: Streamlining your shipping process (This post is part of a series on Making Money with an eBay Store. The Introduction and table of contents to the series is here.) Part 6 of this series on making money with eBay Storesshowed some of the things I do to streamline my listing process. The idea is......



