简体   繁体   中英

How can I use a parameter of a struct type from a struct?

I have declared a parameter of a struct type in a struct and when I try to use that parameter from the struct I get the error NullReferencePointer .

Also I know that you can't initialize a struct in a struct. Is there a way to use that parameter without getting that error?

I want to use the TCommandParam parameter from TCommandBuffer struct.

[StructLayout(LayoutKind.Sequential)]
public struct TCommandParam
{
    public int iValue;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4096)]
    public byte[] sValue;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
    public byte[] FrameFD;       
}

[StructLayout(LayoutKind.Sequential)]
public struct TCommandBuffer
{
    public int Command;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public TCommandParam[] Param;
    public int ReturnValue;
}

Maybe use constructor??Modify second struct as follows:

[StructLayout(LayoutKind.Sequential)]
public struct TCommandBuffer
{
    public int Command;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public TCommandParam[] Param;
    public int ReturnValue;
    public TCommandBuffer(int tsize, int cmd, int ret)
    {
        Param = new TCommandParam[tsize];
        Command = cmd;
        ReturnValue = ret;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM