For each with IDictionary

I've got such code:

Collections::Generic::IDictionary<String*,int> *mapLetters;
for each(Collections::Generic::KeyValuePair<String,int>*q in mapLetters){
myFunction();
}

And the for-each element does not work. I've got an error:
error C3285: for each statement cannot operate on variables of type 'System::Collections::Generic::IDictionary<TKey,TValue> __gc *'

But in official site of Microsoft they wrote that for-each must work fine.
There's the note there:
--
The foreach statement of the C# language (For Each in Visual Basic, for each in C++) requires the type of each element in the collection. Since each element of the IDictionary is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is KeyValuePair. For example:
C++:
for each (KeyValuePair<int, String^> kvp in myDictionary) {...}
--
Here's the link:
http://msdn2.microsoft.com/en-us/library/s4ys34ea(VS.80).aspx

Something is wrong. How can I use for-each with IDictionary-element




Answer this question

For each with IDictionary

  • Sridhar Paladugu

    Just for other folks on the forums, Here is a link to the bug: http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx feedbackid=7dabbf4b-cc28-4cb3-b0cf-14100a949ce6

    Thanks,
    Ayman Shoukry
    VC++ Team


  • ssmile73

    MsFit, could you log a bug at http://lab.msdn.microsoft.com/productfeedback/default.aspx so that the owners take a look straight ahead.

    Thanks,
    Ayman Shoukry
    VC++ Tean


  • tricky_richy

    Can someone at microsoft comment

    It looks like for each is either unavailable or unusable in /clr:oldSyntax (which means there is a documentation error or a bug in the Microsoft C++ compiler). I've tried using for each in /clr:oldSyntax

    __gc class A {
    public:

      static void B(void)
      {

        int NumArray __gc[] = new int __gc[5];

        for each(int q in NumArray)
        {
          System::Console::WriteLine(q);
        }

      }

    };

    and I get the following errors

    .\Dictionary.cpp(8) : error C3083: 'cli': the symbol to the left of a '::' must be a type
    .\Dictionary.cpp(8) : error C2039: 'safe_cast' : is not a member of '`global namespace''
    .\Dictionary.cpp(8) : error C2065: 'safe_cast' : undeclared identifier
    .\Dictionary.cpp(8) : error C2062: type 'int' unexpected

    The first error is particularly interesting. It indicates it is trying to use the namespace cli (only available in C++/CLI) from within old managed C++. The equivalent class in C++/CLI:

    ref class A {
    public:
      static void B(void)
      {

        cli::array<int> ^NumArray = gcnew cli::array<int>(5);
        for each(int q in NumArray)
        {
          System::Console::WriteLine(q);
        }
      }

    };

    Compiles fine. This indicates that for each is not supported under managed C++ (why aren't you saying so in MSDN, and why doesn't the compiler give a clearer error )

    MsFiT...

    you should make it a priority to transition to C++/CLI if you can. The for each construct DOES work under C++/CLI. If that is not an option, try using the TryGetValue method (sorry, I'm not familiar with how to box/unbox values, So I'm going to cheat and use a slightly different IDictionary):

    System::Collections::Generic::IDictionary<int, int> *idict;

    for(int i = 0; i < idict->Count; ++i)
    {

      int ValRes = 0;
      idict->TryGetValue(i, &ValRes);
      System::Console::Write(ValRes);

    }

     



  • happyt

    But these variants don't work too:

    Collections::Generic::IDictionary<String*,int> * mapLetters;
    for
    each(Collections::Generic::KeyValuePair<String*,int>*& q in mapLetters){}



  • CyberDogg

    Couldn't you help me to write a line of code which will use for-each construct with an IDictionary



  • Diesel31

    MsFiT wrote:

    I've got such code:

    Collections::Generic::IDictionary<String*,int> *mapLetters;
    for each(Collections::Generic::KeyValuePair<String,int>*q in mapLetters){
    myFunction();
    }

    And the for-each element does not work. I've got an error:
    error C3285: for each statement cannot operate on variables of type 'System::Collections::Generic::IDictionary<TKey,TValue> __gc *'

    But in official site of Microsoft they wrote that for-each must work fine.
    There's the note there:
    --
    The foreach statement of the C# language (For Each in Visual Basic, for each in C++) requires the type of each element in the collection. Since each element of the IDictionary is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is KeyValuePair. For example:
    C++:
    for each (KeyValuePair<int, String^> kvp in myDictionary) {...}
    --
    Here's the link:
    http://msdn2.microsoft.com/en-us/library/s4ys34ea(VS.80).aspx

    Something is wrong. How can I use for-each with IDictionary-element

    Although he wrote it specifically for C++/CLI, nish has already answered this question: http://blog.voidnish.com/ p=99.

    To find out how to pass-by-reference types in /clr:oldSyntax, see the last question in http://msdn.microsoft.com/msdnmag/issues/06/01/CAtWork/default.aspx.

    If the compiler still complains, take a close look at the comments made in nish's blog posting.



  • For each with IDictionary