Understanding IEnumerable in .Net COM Interop

Why can I use the VBScript for each statement to iterate over the System.Collections.ArrayList oject, but not the Systems.Collections.SortedList ?

Given the following:

 set aList = Server.CreateObject("System.Collections.ArrayList") aList.Add "a" aList.Add "b" aList.Add "c" for each item in aList ' do something next set sList = Server.CreateObject("System.Collections.SortedList") sList.Add "a", 1 sList.Add "b", 2 sList.Add "c", 3 for each item in sList ' do something next 

Line

 for each item in sList 

glitch

An object

does not support this property or method *.

Through this property, I assume that they mean the _NewEnum property. But why does _NewEnum display an ArrayList but not a SortedList ? Both classes implement the IEnumberable interface, which, from the disassembly of mscorelib.dll appears to be the interface responsible for implementing the _NewEnum ( dispId of -4) property.

If someone can shed light on the various COM interactions of these similar classes, I would really appreciate it.

I know that I can use other properties set by SortedList to iterate over the collection. I am not asking how to iterate SortedList . I'm just asking why IEnumrable doesn't seem to be implemented in the interop version of SortedList when it is implemented in the interop version of ArrayList .

+7
source share
1 answer

Although the SortedList implements IEnumerable, it does have an overloaded GetEnumerator () method that returns an IDictionaryEnumerator. You must explicitly specify IEnumerable to use overloading, which returns an IEnumerator where your problem might be.

The default enumerator does not have the same behavior as ArrayList - it will return a DictionaryEntry for each element, not the string that you expect.

I assume that you most likely want to use the "Values" property, and if you sort by numbers, you want to use the "Add Method" arguments in a different way.

 sList.Add 1, "a" 
+3
source

All Articles