3

I need help about array of structs initiliazation. In a code something like below, how we can accomplish the initiliazation defined in comment ??

class structExample
{
    struct state{
        int previousState;
        int currentState;
    }
     static state[] durum;

     public static void main(String[] args)
     {
         durum = new state[5];

         // how we can assign new value to durum[0].previousState = 0; doesn't work ??


     }

}

}

Thanks..

dnur
  • 709
  • 3
  • 13
  • 21
  • 1
    [Mutable structs are evil](http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil) – digEmAll Apr 01 '11 at 21:39

2 Answers2

6

The default accessibility for members in C# is private which is why the assignment statement is failing. You need to make the fields accessible by having adding internal or public to them.

struct state{
    internal int previousState;
    internal int currentState;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

durum = new state[5]; -> creates only the array for 5 elements.

You need to initialize every element inside the array.

Luzik
  • 276
  • 3
  • 8