S
Steve Richter
I have a struct that uses LayoutKind.Explicit to redefine bytes within
the struct:
[StructLayout(LayoutKind.Explicit)]
public struct PointerStoredValue
{
[FieldOffset(0)]
public int LayoutId;
[FieldOffset(4)]
public int HeapId ;
[FieldOffset(8)]
public int BufferBx;
[FieldOffset(12)]
public int EmptySpace;
[FieldOffset(0)]
public long Part1;
[FieldOffset(8)]
public long Part2;
public PointerStoredValue(byte[] InArray, int InBx)
{
Part1 = BitConverter.ToInt64(InArray, InBx);
Part2 = BitConverter.ToInt64(InArray, InBx + 8);
}
}
the constructor will not compile:
"field LayoutId must be fully assigned before control leaves the
constructor"
the Part1 field is a long from bytes 0 to 7 of the struct. field
LayoutId is an int from bytes 0 to 3. By assigning to field Part1,
field LayoutId is being assigned.
Why the compiler error?
thanks,
-Steve
the struct:
[StructLayout(LayoutKind.Explicit)]
public struct PointerStoredValue
{
[FieldOffset(0)]
public int LayoutId;
[FieldOffset(4)]
public int HeapId ;
[FieldOffset(8)]
public int BufferBx;
[FieldOffset(12)]
public int EmptySpace;
[FieldOffset(0)]
public long Part1;
[FieldOffset(8)]
public long Part2;
public PointerStoredValue(byte[] InArray, int InBx)
{
Part1 = BitConverter.ToInt64(InArray, InBx);
Part2 = BitConverter.ToInt64(InArray, InBx + 8);
}
}
the constructor will not compile:
"field LayoutId must be fully assigned before control leaves the
constructor"
the Part1 field is a long from bytes 0 to 7 of the struct. field
LayoutId is an int from bytes 0 to 3. By assigning to field Part1,
field LayoutId is being assigned.
Why the compiler error?
thanks,
-Steve