开发者

How to Assign the value to Main /Parent Structure in a Nested Structure?

Hi I have writen an Nested Structure in C# . Find the code snippet below:

public struct SMB_MESSAGE
        {

            #region SMB Parameter
            public struct SMB_PARAMETERS
            {
                public byte WordCount;
                public ushort[] Words;
            }
            #endregion

            #region SMB Data
            public struct SMB_DATA
            {
                public ushort ByteCount;
                public struct Bytes
                {
                    public ushort BufferFormat;
                    public byte[] Name;
                }
            }
            #endregion

        }

Now While I assign the Value to the the Inner structure as below:

SMB_MESSAGE SMBMESSAGE;

SMB_MESSAGE.SMB_PARAMETERS SMBPARAMETER;
SMBPARAMETER.WordCount=12;
SMBPARAMETER.Words=null;

SMB_MESSAGE.SMB_DATA SMBDATA;
SMBDATA.ByteCount=byteCount;

SMB_MESSAGE.SMB_DATA.Bytes bytes;
bytes.BufferForma开发者_运维知识库t=bFormat;
bytes.Name=name;

Now When I look into the value of SMBMESSAGE while debugging it shows NameSpace.Form1.SMB_MESSAGE and no values inside it. I can't also see a way to asign the values to SMBMESSAGE .

If we can not assign values , then Why do we need to use nested structures?


Your two inner structs are nested types, not instance members.
Nested types have no effect on instances of the parent types; they are a purely organizational concept (except that they can access the parent type's private members)

Therefore, your SMB_MESSAGE struct doesn't actually have any instance members.

You need to make four normal structs, then make two properties in SMB_MESSAGE holding the other two structures.

For example:

public struct SMB_MESSAGE {
    public SMB_PARAMETERS Parameters;
    public SMB_DATA Data;

}
public struct SMB_PARAMETERS
{
    public byte WordCount;
    public ushort[] Words;
}

public struct SMB_DATA
{
    public ushort ByteCount;
    public Bytes Bytes;
}
public struct Bytes
{
    public ushort BufferFormat;
    public byte[] Name;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜