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. how to immigrate c++'s union?

how to immigrate c++'s union?

Scheduled Pinned Locked Moved C#
c++csharptutorialquestion
4 Posts 3 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I just wanna transfer a piece of code from cpp code to csharp. To my surprisse, csharp doesn't have the union key word. Then how can cope with the union in cpp to keep the code change minimized. any suggestion?

    P J 2 Replies Last reply
    0
    • L Lost User

      I just wanna transfer a piece of code from cpp code to csharp. To my surprisse, csharp doesn't have the union key word. Then how can cope with the union in cpp to keep the code change minimized. any suggestion?

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      C# is a type safe language, and a union is every thing that a typesafe language is not. Therefore you will have to create a conversion operator or function to cast from type A to type B.

      1 Reply Last reply
      0
      • L Lost User

        I just wanna transfer a piece of code from cpp code to csharp. To my surprisse, csharp doesn't have the union key word. Then how can cope with the union in cpp to keep the code change minimized. any suggestion?

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

        If you need to interop with a C/C++ object that uses a union you can do that, but there isn't a way to create a union in C# that behaves the same way (well easily create a union, you can re-create the VT_VARIANT type by using an enum and a lot of casting). James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

        L 1 Reply Last reply
        0
        • J James T Johnson

          If you need to interop with a C/C++ object that uses a union you can do that, but there isn't a way to create a union in C# that behaves the same way (well easily create a union, you can re-create the VT_VARIANT type by using an enum and a lot of casting). James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

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

          Well, I try to use set and get to simulate the union. It seems work in my eye. class A { public int a; } class B { public char b; } class union { object obj; public A aa { get { return (A)obj; } set { obj=value; } } public B bb { get { return (B)obj; } set { obj=value; } } }

          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