C# Array Troubles....

Started by
2 comments, last by TheBluMage 19 years, 3 months ago
I'm having some problems following understanding C# arrays:

// Inside Render
		for(int iY = 0; iY < 3; ++iY)
			{
				Console.WriteLine(" {0} | {1} | {2} ", m_cBoard[0, iY], m_cBoard[1, iY], m_cBoard[2, iY]);
				if(iY != 2)
					Console.WriteLine("---|---|---");
			}

// In Class, outside Render

		private char[,] m_cBoard = new char[3, 3] { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} };


I end up with: 147 258 369 though, instead of: 123 456 789 I don't understand why though? 0,0 would be top left to me. 1,0 would be top middle. 2,0 would be top right. Just like on a graph or whatever, but it's like it's making: 0,0 top left 0,1 top middle 0,2 top right Which is like some sort of sideways graph. Am I right? Why does it do it this way? I'm writing a Tic Tac Toe game if that changes anything.
"Where genius ends, madness begins."Estauns
Advertisement
You've got the X's and Y's backwards. Here's how the program sees it:

//                                        0,0  0,1  0,2  1,0  1,1  1,2  2,0  2,1  2,2private char[,] m_cBoard = new char[3, 3] { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} };
Fair enough but why does it do it that way? Doesn't that seem backwards to anyone else?
"Where genius ends, madness begins."Estauns
Conceptually (I don't think you can actually define arrays this way in code) it's like saying this:

m_cBoard[0, *] = {1, 2, 3};
m_cBoard[1, *] = {4, 5, 6};
m_cBoard[2, *] = {7, 8, 9};

Each level of braces gets deeper into the array's rank and the highest level rank comes first, then sub-ranks follow:

private char[,] m_cBoard = new char[,] {/*Array 0:*/ {1, 2, 3}, /*Array 1:*/ {4, 5, 6}, /*Array 2:*/ {7, 8, 9}};

This topic is closed to new replies.

Advertisement