为什么在 struct 中使用 struct array 时会出现错误?
public struct Tables
{
public string name;
}
public struct Schema
{
public string name;
public Tables[] tables; //Declarate struct array in struct
}
我在这段代码中使用了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 :(得分: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
}