2012-11-15 25 views
0

爲什麼我在使用時出現錯誤struct array in struct如何使用數組成員?

public struct Tables 
{ 
    public string name; 
} 

public struct Schema 
{ 
    public string name; 
    public Tables[] tables; //Declarate struct array in struct 
} 

我使用了結構與此代碼:

Schema schema; 

private void Form1_Load(object sender, EventArgs e) 
{ 
    schema.name = "schemaName"; 
    schema.tables[0].name = "tables1Name"; // Error in here: Object reference not set to an instance of an object 
} 
+0

我已經從標題中刪除了對結構的提及......如果不同意,請隨時恢復編輯。 –

回答

2

需要初始化您的數組:

schema.name = "schemaName"; 
schema.tables = new Tables[10]; 
schema.tables[0].name = "tables1"; 
+1

好,謝謝先生。 – YD4

2

這是因爲schema.tables從未初始化,因此是無效

嘗試

private void Form1_Load(object sender, EventArgs e) 
{ 
    schema.name = "schemaName"; 
    schema.tables = new Tables[5]; 
    schema.tables[0].name = "tables1"; // Error ini here: Object reference not set to an instance of an object 
}