2011-04-01 107 views
3

我需要關於數組結構initiliazation的幫助。在下面的代碼中,我們如何完成評論中定義的initiliazation?在c中使用數組的結構#

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 ?? 


    } 

} 

}

謝謝..

+1

[可變的結構是邪惡的](http://stackoverflow.com/questions/441309/why-are-mutable-structs-vil) – digEmAll 2011-04-01 21:39:37

回答

6

默認的輔助在C#中的成員是私有的這就是爲什麼賦值語句失敗。您需要通過向其添加internalpublic來訪問這些字段。

struct state{ 
    internal int previousState; 
    internal int currentState; 
} 
+1

你的答案更正確:) – 2011-04-01 21:50:17

0

durum = new state [5]; - >僅爲5個元素創建數組。

您需要初始化數組內的每個元素。

+0

沒有必要,只是試過:) – 2011-04-01 21:51:36