0

For a practice PacMan

array<bool>^ aEtats;             //declared: an array of true/false states

aEtats = gcnew array<bool>(100); //this array will correspond with an array of "Pills"

for each (bool b in aEtats)  
     b=true;

I get an array of 100 "false". Why ?

2 Answers 2

1

The bool type is a value type, you get a copy of the value in the for-each statement, not a reference. So you are setting the copy to true, this doesn't propagate back to the array element. Use a simple for loop instead:

Etats = gcnew array<bool>(100);
for (int ix = 0; ix < Etats->Length; ++ix)
    Etats[ix] = true;
Sign up to request clarification or add additional context in comments.

2 Comments

Or just change the for each loop to use a reference, e.g. for each (bool% b in aEats).
ok,thanks works, used the second solution offered since the goal is to familiarize with C++/CLI management. You should see my other question: similar confusion!
1

Or just change the for each loop to use a reference, e.g. for each (bool% b in aEats). – Ben Voigt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.