Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. unions

unions

Scheduled Pinned Locked Moved C#
questioncsharp
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    How do I emulate a union structure in C#?

    N 1 Reply Last reply
    0
    • A Anonymous

      How do I emulate a union structure in C#?

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      Anonymous wrote: How do I emulate a union structure in C#? Note the attributes StructLayout and FieldOffset.

      [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

      G N 2 Replies Last reply
      0
      • N Nick Parker

        Anonymous wrote: How do I emulate a union structure in C#? Note the attributes StructLayout and FieldOffset.

        [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

        G Offline
        G Offline
        grv575
        wrote on last edited by
        #3

        Thanks. So for the BE_CONFIG union in the LAME api I should just start every inner struct at FieldOffset(0)?

        1 Reply Last reply
        0
        • N Nick Parker

          Anonymous wrote: How do I emulate a union structure in C#? Note the attributes StructLayout and FieldOffset.

          [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

          N Offline
          N Offline
          Nathan Blomquist
          wrote on last edited by
          #4

          Shouldn't the Composite member be of type Int32 (int)? Longs in C# are aliases to Int64s. [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?

          N G 2 Replies Last reply
          0
          • N Nathan Blomquist

            Shouldn't the Composite member be of type Int32 (int)? Longs in C# are aliases to Int64s. [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?

            N Offline
            N Offline
            Nick Parker
            wrote on last edited by
            #5

            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 to System.Int64 I am o.k. :) -Nick Parker

            1 Reply Last reply
            0
            • N Nathan Blomquist

              Shouldn't the Composite member be of type Int32 (int)? Longs in C# are aliases to Int64s. [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?

              G Offline
              G Offline
              grv575
              wrote on last edited by
              #6

              Yeah what exactly does cls compliant really mean? Is that the same as a Microsoft Certified Professional?

              J 1 Reply Last reply
              0
              • G grv575

                Yeah what exactly does cls compliant really mean? Is that the same as a Microsoft Certified Professional?

                J Offline
                J Offline
                James T Johnson
                wrote on last edited by
                #7

                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 and public 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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups