0

I'm trying to do what the question says. (It might be confusing) Here is some code that should make you understand what I'm trying to do.

classes = new string[14, 5];
classes[0] = {"Value1 [0, 0]", "Value2 [0, 1]", "Value3 [0, 2]", "Value4 [0, 3]", "Value5 [0, 4]"};
Daniel Hammer
  • 39
  • 1
  • 5

1 Answers1

1

Some languages or environments(like Matlab) allow such things to work, but C# doesn't provide such access for rectangular arrays String[x, y].

With such arrays you should change each element individually:

String[,] classes = new string[14, 5];classes = new string[14, 5];

Int32 rowToChange = 0;

for(Int32 col = 0; col < classes.GetLength(1); col++)
{
   classes[rowToChange, col] = String.Format("Value{0} [{1}. {0}]", rowToChange , col  );
}

But you could use jagged arrays : String[][]

String[][] classes = new string[14][];

Int32 rowToChange = 0;

classes[rowToChange] = new String[]{"Value1 [0, 0]", "Value2 [0, 1]", "Value3 [0, 2]", "Value4 [0, 3]", "Value5 [0, 4]"};

You could read What are the differences between a multidimensional array and an array of arrays in C#? to understand the differences

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • Thx, why does this fail? "classes = new string[14][5];" Same with "String". (Code inline/blocks not supported for comments?) – Daniel Hammer Jun 21 '14 at 18:10
  • Don't know. My snippets work - try them. Maybe the type of 'classes'(how you defined it?) is incorrect. – Eugene Podskal Jun 21 '14 at 18:17
  • Thx man! Your solution of jagged arrays worked! I read the link as well and it turns out that jagged arrays are faster too! Best answer selected :) – Daniel Hammer Jun 21 '14 at 18:24