unions
-
Anonymous wrote: How do I emulate a union structure in C#? Note the attributes
StructLayout
andFieldOffset
.[StructLayout(LayoutKind.Explicit)]
public struct SampleUnion
{
[FieldOffset(0)] public bool Flag1;
[FieldOffset(1)] public bool Flag2;
[FieldOffset(2)] public bool Flag3;
[FieldOffset(3)] public bool Flag4;
[FieldOffset(0)] public long Composite;
}-Nick Parker
-
Anonymous wrote: How do I emulate a union structure in C#? Note the attributes
StructLayout
andFieldOffset
.[StructLayout(LayoutKind.Explicit)]
public struct SampleUnion
{
[FieldOffset(0)] public bool Flag1;
[FieldOffset(1)] public bool Flag2;
[FieldOffset(2)] public bool Flag3;
[FieldOffset(3)] public bool Flag4;
[FieldOffset(0)] public long Composite;
}-Nick Parker
-
Anonymous wrote: How do I emulate a union structure in C#? Note the attributes
StructLayout
andFieldOffset
.[StructLayout(LayoutKind.Explicit)]
public struct SampleUnion
{
[FieldOffset(0)] public bool Flag1;
[FieldOffset(1)] public bool Flag2;
[FieldOffset(2)] public bool Flag3;
[FieldOffset(3)] public bool Flag4;
[FieldOffset(0)] public long Composite;
}-Nick Parker
Shouldn't the
Composite
member be of typeInt32
(int
)?Long
s in C# are aliases toInt64
s. [edit] I guess you could use a long if you'd like because this is a union (also only a sample :)).Just the first four bytes of the long will be used when dealing with the booleans.Also remember that these unions in C# are not CLS compliant. [/edit] -Nathan --------------------------- Hmmm... what's a signature?
-
Shouldn't the
Composite
member be of typeInt32
(int
)?Long
s in C# are aliases toInt64
s. [edit] I guess you could use a long if you'd like because this is a union (also only a sample :)).Just the first four bytes of the long will be used when dealing with the booleans.Also remember that these unions in C# are not CLS compliant. [/edit] -Nathan --------------------------- Hmmm... what's a signature?
Nathan Blomquist wrote: Shouldn't the Composite member be of type Int32 (int)? Longs in C# are aliases to Int64s. I suppose it would be a "best programming practice", however because in C# a
long
maps toSystem.Int64
I am o.k. :) -Nick Parker -
Shouldn't the
Composite
member be of typeInt32
(int
)?Long
s in C# are aliases toInt64
s. [edit] I guess you could use a long if you'd like because this is a union (also only a sample :)).Just the first four bytes of the long will be used when dealing with the booleans.Also remember that these unions in C# are not CLS compliant. [/edit] -Nathan --------------------------- Hmmm... what's a signature?
-
Yeah what exactly does cls compliant really mean? Is that the same as a Microsoft Certified Professional?
grv575 wrote: what exactly does cls compliant really mean? It means that the publicly exposed interface for the member/type/assembly follows the Common Language Specification (CLS). Some things that aren't CLS Complaint: Unsigned integer types (C#), varying public items by changing just the case of the name (like
public int Foo
andpublic int foo
). James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him