Jagged arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Femi

    #1

    Jagged arrays

    Hello,

    Can anyone help? I get the error message below:

    Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

    Code snippet:

    Class Test{

    string [][] MyString = new string [3][];
    MyString[0] = new string[5];
    }

    Thanks


  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Jagged arrays

    Femi wrote:
    Can anyone help? I get the error message below:
    >
    Array size cannot be specified in a variable declaration (try
    initializing with a 'new' expression)
    >
    Code snippet:
    >
    Class Test{
    >
    string [][] MyString = new string [3][];
    MyString[0] = new string[5];
    }
    Those two lines world work inside a method but not outside.

    Arne

    Comment

    • Marc Gravell

      #3
      Re: Jagged arrays

      Further to Arne's reply, you could also use array-initializer syntax
      (below), but it seems unlikely that you would generally know these
      numbers at compile-time; as such, the constructor is a good place to
      consider.

      string[][] MyString = { new string[5], new string[2], new
      string[10] };

      (note: only verified on C#3; my 2.0 machine is unavailable)

      Marc

      Comment

      Working...