Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use foreach in c++ cli in managed code

Tags:

c++-cli

Hi how to use foreach loop in managed code c++ using vs2003.

like image 401
Cute Avatar asked May 12 '09 09:05

Cute


3 Answers

I've never used it, but this MSDN article indicates the general syntax is just:

for each(Type t in IEnumerable)
{

}
like image 60
Matthew Flaschen Avatar answered Nov 06 '22 18:11

Matthew Flaschen


Matthew is mostly correct, but here's a working block of code;

///////
array<Type^>^ iterate_me = gcnew array<Type^>(2);
iterate_me[0] = Type::GetType("Type");
iterate_me[1] = Type::GetType("System.Int32");
///////

for each(Type^ t in iterate_me)
    Console::WriteLine(t);

The changes were Type is a reference class, so you use "Type^" not "Type" and you need an actual object reference (iterate_me)...

like image 21
RandomNickName42 Avatar answered Nov 06 '22 16:11

RandomNickName42


Something like:

String ^ MyString = gcnew String("abcd");

for each ( Char c in MyString )

    Console::Write(c);
like image 31
Aamir Avatar answered Nov 06 '22 18:11

Aamir