VB.NET For Each steps into loop body for an IEnumerable collection! How? Why?
Posted
by Paul Sasik
on Stack Overflow
See other posts from Stack Overflow
or by Paul Sasik
Published on 2010-03-11T17:07:53Z
Indexed on
2010/03/11
17:39 UTC
Read the original article
Hit count: 260
vb.net
|foreach-loop
This is weird. I have a class that inherits from IEnumrable whose Count property is reporting 0 (zero) elements but the For Each loop steps into the loop body and tries to use the variable where it should just be moving on. My code:
On Error Resume Next
Dim d As Foo
For Each d In fooCollection
' use d and throws an exception
Next d
Weirder still, every time d is accessed i get an exception thrown in the output window:
A first chance exception of type 'System.NullReferenceException'
but i'm not stopping on the exception (not in a try/catch block).
Is "On Error Resume Next" causing this weirdness?
Weirdness found:
Per Rowland's and JohnH's comments i checked the Foo class: The GetEnumerator method inside of Foo didn't actually return anything! It had an empty body. That coupled with the On Error Resume Next before the loop caused the havoc! Wow this was ugly. Thanks for the clues guys!
© Stack Overflow or respective owner