IDictionaryEnumerator question

  • Thread starter Thread starter Chris Skaryd
  • Start date Start date
C

Chris Skaryd

Everything I find about IDictionaryEnumerator says that they are readonly.

I'm somewhat confused by the following example. The MsgBox 4 lines from the
bottom will never be run. In the first While loop the Clear is called, but
the value is never added back into the ht object. So why is the hashtable
cleared when I get the to While m block? Shouldn't this not happen since the
Enumerator is readonly?

Thanks in advance for any explanation.

---
Dim ht As New Hashtable()

Dim I As Integer

For I = 1 To 5

Dim h As New Hashtable()

h.Add(I, I)
ht.Add(I, h)

Next I

Dim en As IDictionaryEnumerator = ht.GetEnumerator()

While en.MoveNext
en.Value.clear()
End While

Dim ne As IDictionaryEnumerator = ht.GetEnumerator()

While ne.MoveNext
Dim m As IDictionaryEnumerator = ne.Value.GetEnumerator()

While m.MoveNext
MsgBox(m.Key & "," & m.Value)
End While
End While

End Sub
 
All these references refer to the same collections of objects so when you
cleared them in the While loop, you leave nothing to enumerate through

OHM

Chris said:
Everything I find about IDictionaryEnumerator says that they are
readonly.

I'm somewhat confused by the following example. The MsgBox 4 lines
from the bottom will never be run. In the first While loop the Clear
is called, but the value is never added back into the ht object. So
why is the hashtable cleared when I get the to While m block?
Shouldn't this not happen since the Enumerator is readonly?

Thanks in advance for any explanation.

---
Dim ht As New Hashtable()

Dim I As Integer

For I = 1 To 5

Dim h As New Hashtable()

h.Add(I, I)
ht.Add(I, h)

Next I

Dim en As IDictionaryEnumerator = ht.GetEnumerator()

While en.MoveNext
en.Value.clear()
End While

Dim ne As IDictionaryEnumerator = ht.GetEnumerator()

While ne.MoveNext
Dim m As IDictionaryEnumerator = ne.Value.GetEnumerator()

While m.MoveNext
MsgBox(m.Key & "," & m.Value)
End While
End While

End Sub

Regards - OHM# (e-mail address removed)
 
I sort of figured that was what was happening, but I thought that the
IDictionaryEnumerator was readonly. Shouldn't the Clear method have thrown
an error?
 
Chris,
This seems to be a really bogus example, designed to befuddle the poster and
responder! ;-)
While en.MoveNext
en.Value.clear()
End While

The "en.Value.Clear" is not operating on the Hashtable in the ht variable,
it is calling a method on the class that is referenced in the value
property. (the h Hashtable).

IDictionaryEnumerator is readonly in that you cannot modify the actual
reference returned by the Value, Key, Entry, or Current properties of the
IDictionaryEnumerator variable. You can however modify the objects that
these references refer to!

Remember having a readonly reference does not prevent you from modify the
object that the reference refers to in .NET! It only prevents you from
modifying the reference (field or property) itself.

Try:
While ne.MoveNext

MessageBox.Show(ne.Key.ToString() & "," &
ne.Value.ToString())
Dim m As IDictionaryEnumerator = ne.Value.GetEnumerator()
While m.MoveNext
MsgBox(m.Key & "," & m.Value)
End While
End While

Also I find its generally better to use GetEnumerator with the For Each
statement, as it producers cleaner & safer code.

Hope this helps
Jay
 
Back
Top