views:

33

answers:

1

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.BufferFormat=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?

A: 

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;
}
SLaks
@SLaks,Got It, Just wondering then Why any one Will use nested structure. For which purpose they can be used?
Subhen
Why was this downvoted?
SLaks
@Subhen: http://msdn.microsoft.com/en-us/library/ms229027%28v=VS.100%29.aspx
SLaks