I have this object array called if (b) {
System.out.println("one. b = true");
} else {
System.out.println("two. b = false");
}
, and i want each object in the array go through a function.
This is the code that i have...
blocks
i would like to know on how to replace the object[] blocks = new object[2];
blocks[0] = block;
blocks[1] = block1;
blocks[2] = block2;
if (player.Bottom >= screen.Bottom)
{
jump = false;
jumped = false;
}
else if (player.Right >= i.Left + 5 &&
player.Left <= i.Right - 5 &&
player.Bottom >= i.Top &&
player.Bottom <= i.Bottom - 10)
{
force = 0;
jump = false;
player.Top = i.Location.Y - player.Height;
}
else
{
player.Top += 5;
jumped = false;
nothing = false;
}
with every object in the array i
. So that it will do the function with every single block. Instead of having to make a separate function for every single block ( there will be over 50 objects in original code, this is just a simplified version of it).
答案 0 :(得分:7)
A simple window.open("http://www.w3schools.com");
statement should suffice:
foreach
EDIT: MSDN Documentation has more info and should help you if you get stuck: "Using foreach with Arrays (C# Programming Guide)".