我想將值設置爲我的結構編寫的C#和我得到一個錯誤。用兩個級別數組c填充複雜結構#
private struct pack2
{
public float var;
public float [3]var2;
}
private struct pack
{
public pack2[] ster;
}
private void recv()
{
pack mystruct;
mystruct.ster = new pack2[4]; // Because I need to declare 4 structures of pack2
mystruct.ster[0].var = 2F;
mystruct.ster[0].var[0] = 1F; // error
}
錯誤:NullReferenceException異常是未處理的,ADDINFO:對象引用不設置到對象
我怎麼能解決這個問題的一個實例?
解決:
private struct pack2
{
public float var;
public float [3]var2;
}
private struct pack
{
public pack2[] ster;
}
private void recv()
{
pack mystruct;
mystruct.ster = new pack2[4]; // Because I need to declare 4 structures of pack2
mystruct.ster[0].var = 2F;
mystruct.ster[0].var2 = new float[3]; // Because I need 3 floats on each pack2
mystruct.ster[1].var2 = new float[3]; // Because I need 3 floats on each pack2
mystruct.ster[2].var2 = new float[3]; // Because I need 3 floats on each pack2
mystruct.ster[3].var2 = new float[3]; // Because I need 3 floats on each pack2
mystruct.ster[0].var2[0] = 1F;
mystruct.ster[0].var2[1] = 1F;
mystruct.ster[0].var2[2] = 1F;
}
我希望這將是有用的人。感謝alex
可變的結構是邪惡的。那些應該是階級。 – SLaks 2014-11-03 03:59:16
我想回應SLaks所說的,並提供Microsoft的這種官方指導:[在類和結構之間選擇](http://msdn.microsoft.com/en-us/library/ms229017.aspx) – 2014-11-03 04:02:14