-1

Dont understand what's the problem here, I have been trying to trouble shoot this for half an hour. First time using foreach loop, and I am getting a cannot convert type 'char' to 'char[]' what does this mean? pretty new to programming.

namespace X_O_spel { class Program { static void Main(string[] args) { char[,] spelBoard = new char[,] { {' ', '|', ' ', '|', ' '}, {'-', '+', '-', '+', '-'}, {' ', '|', ' ', '|', ' '}, {'-', '+', '-', '+', '-'}, {' ', '|', ' ', '|', ' '},}; foreach(char[] row in spelBoard) { foreach(char c in row) { Console.Write(c); } } } } } 
1

5 Answers 5

1

You're using a multidimensional array when you really want to be using a jagged array. Change char[,] to char[][] and you're code works:

char[][] spelBoard = new[]{ new[]{' ', '|', ' ', '|', ' '}, new[]{'-', '+', '-', '+', '-'}, new[]{' ', '|', ' ', '|', ' '}, new[]{'-', '+', '-', '+', '-'}, new[]{' ', '|', ' ', '|', ' '}}; foreach(char[] row in spelBoard) { foreach(char c in row) { Console.Write(c); } } 

More information:

Sign up to request clarification or add additional context in comments.

2 Comments

ahhhhhhhhhhhh ok, i understand. Thanks for the help!
@Khaled Glad I could help. You should upvote and accept the answer to mark your question as solved if it helped you :)
1

if you want to get a char array from your result, i believe you would have an easier time just storing rows of strings.

 string[] spelBoard = new string[] {" | | ", "-+-+-", " | | ", "-+-+-", " | | "}; foreach(string row in spelBoard) { Console.WriteLine(row); } 

and of course if you need to, you could still treat row like a char array.

 foreach(char c in row) { Console.Write(c); } 

3 Comments

That doesn't really matter.
you're correct. The initializer isn't compiling.
code is correct (and totally changed) now
0

You're using a multidimensional array as if it were a jagged array.

Multidimensional: matrix.

Jagged: Array of arrays.

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes.

You can solve declaring

char[][] spelBoard = new char { new char[] {' ', '|', ' ', '|', ' '}, new char[] {'-', '+', '-', '+', '-'}, ...}; 

1 Comment

Thanks for your help!
0
 { {' ', '|', ' ', '|', ' '}, {'-', '+', '-', '+', '-'}, {' ', '|', ' ', '|', ' '}, {'-', '+', '-', '+', '-'}, {' ', '|', ' ', '|', ' '}} 

is not char[,] but it is char[][], so change your code like:

 char[][] spelBoard = new char[][] { {' ', '|', ' ', '|', ' '}, {'-', '+', '-', '+', '-'}, {' ', '|', ' ', '|', ' '}, {'-', '+', '-', '+', '-'}, {' ', '|', ' ', '|', ' '}}; 

1 Comment

Thanks for the help!
0

Here is complete working example:

 class Program { static void Main(string[] args) { char[][] spelBoard = { new[]{' ', '|', ' ', '|', ' '}, new[]{'-', '+', '-', '+', '-'}, new[]{' ', '|', ' ', '|', ' '}, new[]{'-', '+', '-', '+', '-'}, new[]{' ', '|', ' ', '|', ' '}}; foreach (char[] row in spelBoard) { foreach (char c in row) { Console.Write(c); } } Console.ReadLine(); } } 

1 Comment

The help is much appreciated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.