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. The Lounge
  3. Unions in C#

Unions in C#

Scheduled Pinned Locked Moved The Lounge
csharpcom
30 Posts 16 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.
  • E Eytukan

    oops just seeing it's not there natively. (Not sure why) but done through interop :)

    using System.Runtime.InteropServices;
    [StructLayout(LayoutKind.Explicit)]
    struct ByteArray {
    [FieldOffset(0)]
    public byte Byte1;
    [FieldOffset(1)]

    Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

    Sander RosselS Offline
    Sander RosselS Offline
    Sander Rossel
    wrote on last edited by
    #16

    var unioned = new[] { "Hi" }.Union(new[] { "I'm unioned" });

    From the top of my head, but it's a union and it should compile :D

    Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

    E 1 Reply Last reply
    0
    • C CodeWraith

      I thought Nobody Screws With The Union[^].

      I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

      R Offline
      R Offline
      RickZeeland
      wrote on last edited by
      #17

      We have Union in the Netherlands :-\ Union is the city bike brand[^]

      1 Reply Last reply
      0
      • E Eytukan

        oops just seeing it's not there natively. (Not sure why) but done through interop :)

        using System.Runtime.InteropServices;
        [StructLayout(LayoutKind.Explicit)]
        struct ByteArray {
        [FieldOffset(0)]
        public byte Byte1;
        [FieldOffset(1)]

        Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #18

        Even C++ is flying away from those dirty boulevards[^].

        In testa che avete, signor di Ceprano?

        E 1 Reply Last reply
        0
        • K kalberts

          Does anyone remember Fortran COMMON blocks? Unions are sort of like that.

          C Offline
          C Offline
          Chris Maunder
          wrote on last edited by
          #19

          My first real programming job was all FORTRAN. 'Cause Real Scientists Program In FORTRAN. It was especially fun when everyone insisted that variables be named a,aa,aaa,b,bb,bbb,b1,bb1 etc. For a bunch of smart people they were truly awful at organising (let along thinking through) code.

          cheers Chris Maunder

          L K 2 Replies Last reply
          0
          • C Chris Maunder

            My first real programming job was all FORTRAN. 'Cause Real Scientists Program In FORTRAN. It was especially fun when everyone insisted that variables be named a,aa,aaa,b,bb,bbb,b1,bb1 etc. For a bunch of smart people they were truly awful at organising (let along thinking through) code.

            cheers Chris Maunder

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #20

            Chris Maunder wrote:

            Cause Real Scientists Program In FORTRAN.

            And you were using it why?

            Michael Martin Australia "I controlled my laughter and simple said "No,I am very busy,so I can't write any code for you". The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So I had to leave the place as soon as possible." - Mr.Prakash One Fine Saturday. 24/04/2004

            C 1 Reply Last reply
            0
            • C Chris Maunder

              My first real programming job was all FORTRAN. 'Cause Real Scientists Program In FORTRAN. It was especially fun when everyone insisted that variables be named a,aa,aaa,b,bb,bbb,b1,bb1 etc. For a bunch of smart people they were truly awful at organising (let along thinking through) code.

              cheers Chris Maunder

              K Offline
              K Offline
              kalberts
              wrote on last edited by
              #21

              My very first programming was in BASIC in those days when it was really BASIC: You had a maximum of 286 numeric variables named A to Z or A0 - A9 to Z0 - Z9, plus 26 string variables A$ to Z$. And then you could have 26 arrays, I believe they were named A# to Z#. I really should dig up that old "Real Time Basic for the Univac 1100 series" manual from my basement, for a nostalgia trip :-) That was in my high school days. At the Univerisity we learned Fortran and then Simula and Pascal, and the professor went to extremes in demanding long, descriptive variable names: When adding two numbers, fitting the names of the sum and the two addends on a single line could be a problem within the 80 char screen width. But the professor insisted. Then, I looked over the shoulder of the brightest guys in the class while he was typing in some Pascal code, and I gasped: You can't hand in that! ... Variables were named I01, I02... F01, F02 (F for Float) and so on. "Of course I won't!" he replied, "Before I hand it in, I have the editor automatically replace I01 with NumberOfApplesPerBasket, F02 with AverageWeightPerAppleInGrams and I02 with NumberOfBasketsPerLoad - I can't waste my time typing those terribly long names every time I use that variable!" -- Sure he was the brightest guy. To him, the mental effort of knowing the meaning of I01 was no greater than knowingt the meaning of NumberOfApplesPerBasket. So why not save a little typing? For my own part, I am happy with descriptive names (within limits), even if it takes a little more typing.

              C 1 Reply Last reply
              0
              • L Lost User

                Chris Maunder wrote:

                Cause Real Scientists Program In FORTRAN.

                And you were using it why?

                Michael Martin Australia "I controlled my laughter and simple said "No,I am very busy,so I can't write any code for you". The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So I had to leave the place as soon as possible." - Mr.Prakash One Fine Saturday. 24/04/2004

                C Offline
                C Offline
                Chris Maunder
                wrote on last edited by
                #22

                You hurt me. You hurt me deep.

                cheers Chris Maunder

                1 Reply Last reply
                0
                • K kalberts

                  My very first programming was in BASIC in those days when it was really BASIC: You had a maximum of 286 numeric variables named A to Z or A0 - A9 to Z0 - Z9, plus 26 string variables A$ to Z$. And then you could have 26 arrays, I believe they were named A# to Z#. I really should dig up that old "Real Time Basic for the Univac 1100 series" manual from my basement, for a nostalgia trip :-) That was in my high school days. At the Univerisity we learned Fortran and then Simula and Pascal, and the professor went to extremes in demanding long, descriptive variable names: When adding two numbers, fitting the names of the sum and the two addends on a single line could be a problem within the 80 char screen width. But the professor insisted. Then, I looked over the shoulder of the brightest guys in the class while he was typing in some Pascal code, and I gasped: You can't hand in that! ... Variables were named I01, I02... F01, F02 (F for Float) and so on. "Of course I won't!" he replied, "Before I hand it in, I have the editor automatically replace I01 with NumberOfApplesPerBasket, F02 with AverageWeightPerAppleInGrams and I02 with NumberOfBasketsPerLoad - I can't waste my time typing those terribly long names every time I use that variable!" -- Sure he was the brightest guy. To him, the mental effort of knowing the meaning of I01 was no greater than knowingt the meaning of NumberOfApplesPerBasket. So why not save a little typing? For my own part, I am happy with descriptive names (within limits), even if it takes a little more typing.

                  C Offline
                  C Offline
                  Chris Maunder
                  wrote on last edited by
                  #23

                  Classic! Why solve a problem when you can create a sub-problem and solve that on top of the main problem. Ah, software developers.

                  cheers Chris Maunder

                  1 Reply Last reply
                  0
                  • E Eytukan

                    oops just seeing it's not there natively. (Not sure why) but done through interop :)

                    using System.Runtime.InteropServices;
                    [StructLayout(LayoutKind.Explicit)]
                    struct ByteArray {
                    [FieldOffset(0)]
                    public byte Byte1;
                    [FieldOffset(1)]

                    Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                    E Offline
                    E Offline
                    englebart
                    wrote on last edited by
                    #24

                    I did like unions for things like: union ColorMashup { long ARGB; struct { byte alpha; byte red; byte green; byte blue; } } where the same data could be accessed as a whole or part. Polymorphism solves most "union" problems a lot better, but the memory will probably be spread around a lot more. You had to squeeze every drop of memory and performance out of the old unix machines.

                    1 Reply Last reply
                    0
                    • E Eytukan

                      oops just seeing it's not there natively. (Not sure why) but done through interop :)

                      using System.Runtime.InteropServices;
                      [StructLayout(LayoutKind.Explicit)]
                      struct ByteArray {
                      [FieldOffset(0)]
                      public byte Byte1;
                      [FieldOffset(1)]

                      Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                      D Offline
                      D Offline
                      DSewhuk
                      wrote on last edited by
                      #25

                      Most useful for hardware registers and even software registers. You can view register broadly via the long/int/short or via the fields contained within it. Save a copy: myCopyReg.bits = mySrcReg.bits. As a bonus your get a copy of the "undefined" bits. Beats myRegCopy.fld0 = myRegSrc.fld0; myRegCopy.fld1 = myRegSrc.fld1; myRegCopy.fld2 = myRegSrc.fld2; et. nauseum

                      1 Reply Last reply
                      0
                      • E Eytukan

                        oops just seeing it's not there natively. (Not sure why) but done through interop :)

                        using System.Runtime.InteropServices;
                        [StructLayout(LayoutKind.Explicit)]
                        struct ByteArray {
                        [FieldOffset(0)]
                        public byte Byte1;
                        [FieldOffset(1)]

                        Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                        J Offline
                        J Offline
                        Jerry W Manweiler Ph D
                        wrote on last edited by
                        #26

                        Thank you for this posting. I am currently working on the conversion of some very old FORTRAN software for the NASA Voyager data processing as we are one of the instrument teams still working the Voyager Interstellar Mission and I have been struggling to find an option in C# to do exactly this, i.e. as mentioned in a reply this is like the FORTRAN COMMON BLOCK statement. Now I think I will be able to finish my conversion project a lot easier than attempting to write my own version of a COMMON statement and figuring out how to implement all of the various possibilities.

                        Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

                        E 1 Reply Last reply
                        0
                        • CPalliniC CPallini

                          Even C++ is flying away from those dirty boulevards[^].

                          E Offline
                          E Offline
                          Eytukan
                          wrote on last edited by
                          #27

                          You should be supporting the real men way of doing things bro. :)

                          Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                          CPalliniC 1 Reply Last reply
                          0
                          • J Jerry W Manweiler Ph D

                            Thank you for this posting. I am currently working on the conversion of some very old FORTRAN software for the NASA Voyager data processing as we are one of the instrument teams still working the Voyager Interstellar Mission and I have been struggling to find an option in C# to do exactly this, i.e. as mentioned in a reply this is like the FORTRAN COMMON BLOCK statement. Now I think I will be able to finish my conversion project a lot easier than attempting to write my own version of a COMMON statement and figuring out how to implement all of the various possibilities.

                            Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

                            E Offline
                            E Offline
                            Eytukan
                            wrote on last edited by
                            #28

                            :) :thumbsup:

                            Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                            1 Reply Last reply
                            0
                            • Sander RosselS Sander Rossel

                              var unioned = new[] { "Hi" }.Union(new[] { "I'm unioned" });

                              From the top of my head, but it's a union and it should compile :D

                              Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

                              E Offline
                              E Offline
                              Eytukan
                              wrote on last edited by
                              #29

                              :cool::thumbsup: :)

                              Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                              1 Reply Last reply
                              0
                              • E Eytukan

                                You should be supporting the real men way of doing things bro. :)

                                Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                                CPalliniC Offline
                                CPalliniC Offline
                                CPallini
                                wrote on last edited by
                                #30

                                You know, I am "lost in a haze of alcohol soft middle age". ;)

                                In testa che avete, signor di Ceprano?

                                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