07.10.2002 10:30 AM

I'm not a baseball fan by any means, but...you've got to be kidding me, right? Bud Selig, you are an idiot.

"Fight Club" == "Calvin and Hobbes"? You decide.

Hmmm. I always thought foreach in C# and For Each in VB .NET would return objects of the specified type and skip over ones that don't match. I should've read the documentation. Consider the following code:

ArrayList al = new ArrayList();
al.Add(new Guid());
al.Add(new Random());
al.Add(new Guid());
al.Add(new Random());
al.Add(new Guid());
al.Add(new Random());
al.Add(new Guid());
al.Add(new Random());

foreach(Guid g in al)
{
    Console.WriteLine("Found a Guid object.");
}

It will throw an InvalidCastException. I know how to get around this - here's some code in VB .NET to illustrate:

Dim oGUIDs As IEnumerator = al.GetEnumerator()

Do While oGUIDs.MoveNext()
    If TypeOf oGUIDs.Current Is Guid Then
        Dim aGUID as Guid = CType(oGUIDs.Current, Guid)
        '  Do something with aGuid...
    End If
Loop

So much for assuming!

* Posted at 07.10.2002 10:30:00 AM CST | Link *

Blog History