1
namespace text_test
{
public class txt_program


{
    public class txt
    {

        /* 0 */
        int[] M_array_0 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 1 */
        int[] M_array_1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 2 */
        int[] M_array_2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 3 */
        int[] M_array_3 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 4 */
        int[] M_array_4 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 5 */
        int[] M_array_5 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 6 */
        int[] M_array_6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 7 */
        int[] M_array_7 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 8 */
        int[] M_array_8 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 9 */
        int[] M_array_9 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 10 */
        int[] M_array_10 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 11 */
        int[] M_array_11 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 12 */
        int[] M_array_12 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 13 */
        int[] M_array_13 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        int[][] M = { M_array_0, M_array_1 };

    }
}

}

An error appears associated with the section:

int[][] M = { M_array_0, M_array_1 };

The error concerns M_array_0 and M_array_1 in the above. What I dont understant is why I cant create a multidimensional array from the above. That code should I use? I tried:

string[][] M = { M_array_0, M_array_1 };
double[][] M = { M_array_0, M_array_1 };

The error reads:

A field initializer cannot reference the non-static field, method, or property 'txt_program.txt.M_array_0' text_test \psf\Home\Documents\Visual Studio 2015\Projects\text_test\text_test\text.cs 45 Active

On advance thank you.

3 Answers3

1

One way is to write a static method that returns the array that you want, and use that to assign the field:

public class txt
{
    private int[][] M = createArray();

    private static int[][] createArray()
    {
        /* 0 */
        int[] M_array_0 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 1 */
        int[] M_array_1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 2 */
        int[] M_array_2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        /* 3 */

        // Etc

        return new [] { M_array_0, M_array_1, M_array_2 /* etc */ };
    }
}

Alternatively you can write it inline like so (but this is less flexible):

public class txt
{
    private int[][] M = 
    {
        new[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // M_array_0
        new[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // M_array_1
        new[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // M_array_3

        // Etc
    };
}

Since you are initialising all the arrays to the same size, and containing zeroes, you can shorten that to:

public class txt
{
    int[][] M = 
    {
        new int[14], // M_array_0
        new int[14], // M_array_1
        new int[14], // M_array_2

        // Etc
    };
}

However, I suspect that was just an example you gave, and in real code you would be using non-zero values.

Note also that you can declare M as readonly:

private readonly int[][] M = ...

which I recommend that you do, unless you need to change the array itself (rather than the contents) after the class instance is created.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

A field initializer cannot reference the nonstatic field, method, or property 'field' Instance fields cannot be used to initialize other instance fields outside a method. If you are trying to initialize a variable outside a method, consider performing the initialization inside the class constructor

Reference

Also see this for a descriptive answer.

Community
  • 1
  • 1
Techidiot
  • 1,921
  • 1
  • 15
  • 28
0

I suggest using Linq in order to generate the array

int[][] M = Enumerable.Range(0, 13) // 13 rows
  .Select(x => new int[13])         // each line contains 13 items
  .ToArray();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215