Hi Bob,
Sorry for letting you wait.
C++/CLI "for each" statement iterates over objects that implement the
IEnumerable interface, while gcroot does not implement this interface.
Luckily, gcroot provides an operator T conversion, so you can just
static_cast in your example to the MyItem^ type. I have written a sample
using System::String^ type to demonstrate it.
// The following generates the same error as your program
#include <vcclr.h>
using namespace System;
class CppClass {
public:
gcroot<String^s tr; // can use str as if it were String^
CppClass() {}
};
int main(array<Syst em::String ^^args)
{
CppClass c;
c.str = gcnew String("hello") ;
for each ( Char c in c.str )
Console::Write( c);
return 0;
}
Error 1 error C3285: for each statement cannot operate on variables of type
'gcroot<T>'
// The following code compiles well.
#include <vcclr.h>
using namespace System;
class CppClass {
public:
gcroot<String^s tr; // can use str as if it were String^
CppClass() {}
};
int main(array<Syst em::String ^^args)
{
CppClass c;
c.str = gcnew String("hello") ;
for each ( Char c in static_cast<Str ing^>(c.str) )
Console::Write( c);
return 0;
}
Hope it helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== ===========
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.
This posting is provided "AS IS" with no warranties, and confers no rights.