The FieldOffset attribute defines the number of bytes from the start of the
structure where this field will be layed out in memory.
In your case avalue and b value are unsigned chars which take 1 byte so they
will be layed out at mem locations 0 and 1. cvalue is a unsigned short since
it is a union that will also be layed out at offset 0 from the start of the
structure and take 2 bytes.
Basically FieldOffset defines the number of bytes from the start of the
structure where the field will be placed.
Let me know if you need more hlp.
--
Sijin Joseph
http://www.indiangeek.net http://weblogs.asp.net/sjoseph
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:24**********************************@microsof t.com...
Thanks Sijin, i had 1 question.
How do i know what argument to be past in here:
FieldOffset(0) or (1) or (16)
Hmm, can you give me some examples? I can't find proper explanation even
on msdn.
Thanks a lot. :) Really appreciate it.
"Sijin Joseph" wrote:
Try this
[StructLayout(LayoutKind.Explicit)]
public struct Message
{
[StructLayout(LayoutKind.Explicit)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}
[FieldOffset(0)]public AStruct a;
[StructLayout(LayoutKind.Explicit)]
public struct BStruct
{
[FieldOffset(0)] public System.UInt16 cvalue;
}
[FieldOffset(0)]public BStruct b;
}
--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:73**********************************@microsof t.com... Hi there,
I had a union in C:
typedef union TagMessage
{
struct {
unsigned char avalue;
unsigned char bvalue;
} a;
struct {
unsigned short cvalue;
} b;
} Message;
From these few links, there is a possible way is to declare a struct
with LayoutKind.Explict and use FieldOffset.
http://www.dotnet247.com/247referenc.../18/93017.aspx
http://www.hanselman.com/blog/PermaLink.aspx?guid=187
The problem is how do i relate the FieldOffset in my scenarios. I had
2 structs in my C union. How do i achieve it with the help of that?
Any idea please? Thanks.
--
Regards,
Chua Wen Ching :)