I love when customers find bugs!

Reader Rob Walker asks:

Is there a neat way of handling dictionaries? I have a Dictionary<Guid, Object^> and want to iterate over the values.

Currently I have to use the syntax:

for each(KeyValuePair<Guid, Object^> v in dict)
{
v.Value ...
}

I can't find an invocation that would allow

for each (Object^ v in dict->Values)

(This is using the Whidbey beta 1 compiler refresh).

I imagine, Rob, that you're getting what I'm getting from the compiler, which is:

   error C3285: for each statement cannot operate on variables of type 'overloaded-function'

 

That just doesn't seem right to me. In fact, I made an incredibly simple case, and tried it with a very recent compiler:

using namespace System;

 

ref struct R{
property array<int>^ p;
};

int main(){
R^ r = gcnew R;
for each( int i in r->p ){}
}

And this also gives me that same error.  I talked with the guy responsible for testing for each, and he confirmed my suspicion - that's a bug.  Good catch!  I've filed it in our bug tracking database, and shot it over to the proper developer.  (With a note that this bug came from a customer - which always raises bug priority.)

 

In the meantime, there's a pretty simple workaround.  You should be able to get around this problem by calling the property accessor method directly.  In my case, that'd be r->p::get(), and in your case, it'd be dict->Values::get().  That should allow you to use for each on that collection.

 

By the way, I'm sort-of touching on bits of the property syntax which I haven't discussed yet in this blog.  I'll try to get back to that subject after DF.