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. C# Noob in trouble with enumerations

C# Noob in trouble with enumerations

Scheduled Pinned Locked Moved C#
csharphelpquestion
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.
  • W Offline
    W Offline
    Wingnut74
    wrote on last edited by
    #1

    I am a moderately-skilled VB.NET guy who has (very) recently moved to C#. Where I seem to be having trouble is whenever I expect the same sort of forgiveness that VB seemed to give. It seems that enums in C# are not as flexible as they were in VB. I am not sure if this true, but I am struggling where I did not expect to. I have an enum that has int values. Basically, it is like this: public enum Things {thing0 = 0, thing1 = 1, thing2 = 2} ... and so forth. What I can't do is this: int numericThing = 1; Things aThing; aThing = numericThing; // should be thing1, right? This throws a CS0029 error about how it cannot implicitly convert type int to type Things. What am I missing? I mean, if it doesn't work this way? What's the point? I do not want to curl into the fetal position and crawl back to VB...

    D D 2 Replies Last reply
    0
    • W Wingnut74

      I am a moderately-skilled VB.NET guy who has (very) recently moved to C#. Where I seem to be having trouble is whenever I expect the same sort of forgiveness that VB seemed to give. It seems that enums in C# are not as flexible as they were in VB. I am not sure if this true, but I am struggling where I did not expect to. I have an enum that has int values. Basically, it is like this: public enum Things {thing0 = 0, thing1 = 1, thing2 = 2} ... and so forth. What I can't do is this: int numericThing = 1; Things aThing; aThing = numericThing; // should be thing1, right? This throws a CS0029 error about how it cannot implicitly convert type int to type Things. What am I missing? I mean, if it doesn't work this way? What's the point? I do not want to curl into the fetal position and crawl back to VB...

      D Offline
      D Offline
      David M Kean
      wrote on last edited by
      #2

      You need to cast it to Things: Things aThing = (Things)numericThing;

      1 Reply Last reply
      0
      • W Wingnut74

        I am a moderately-skilled VB.NET guy who has (very) recently moved to C#. Where I seem to be having trouble is whenever I expect the same sort of forgiveness that VB seemed to give. It seems that enums in C# are not as flexible as they were in VB. I am not sure if this true, but I am struggling where I did not expect to. I have an enum that has int values. Basically, it is like this: public enum Things {thing0 = 0, thing1 = 1, thing2 = 2} ... and so forth. What I can't do is this: int numericThing = 1; Things aThing; aThing = numericThing; // should be thing1, right? This throws a CS0029 error about how it cannot implicitly convert type int to type Things. What am I missing? I mean, if it doesn't work this way? What's the point? I do not want to curl into the fetal position and crawl back to VB...

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        Wingnut74 wrote: What am I missing? I mean, if it doesn't work this way? What's the point? I do not want to curl into the fetal position and crawl back to VB... As mentioned, you need to cast it to int. And, IMO, this is one of the C# strenghts over VB. Notice, VB allows you to do things that may cause hard to find runtime bugs, while C# will stop them at the moment you're compiling. This is called a "strongly-typed language". notice this code: int numericThing = 1; Things aThing; aThing = (Things)numericThing; Now, it compiles, but does it work? Not always. What if numericThing has a value that is an invalid Thing? You should validate it before assigning. And that's why the C# compiler stopped you. Yes, even I am blogging now!

        W L 2 Replies Last reply
        0
        • D Daniel Turini

          Wingnut74 wrote: What am I missing? I mean, if it doesn't work this way? What's the point? I do not want to curl into the fetal position and crawl back to VB... As mentioned, you need to cast it to int. And, IMO, this is one of the C# strenghts over VB. Notice, VB allows you to do things that may cause hard to find runtime bugs, while C# will stop them at the moment you're compiling. This is called a "strongly-typed language". notice this code: int numericThing = 1; Things aThing; aThing = (Things)numericThing; Now, it compiles, but does it work? Not always. What if numericThing has a value that is an invalid Thing? You should validate it before assigning. And that's why the C# compiler stopped you. Yes, even I am blogging now!

          W Offline
          W Offline
          Wingnut74
          wrote on last edited by
          #4

          Daniel Turini wrote: Now, it compiles, but does it work? Not always. What if numericThing has a value that is an invalid Thing? You should validate it before assigning. And that's why the C# compiler stopped you. Thanks to both of you. I figured it was something I was missing and was related to the way I have become used to VB's mommy-coddling. I am still a bit out of my comfort zone, but I see the truth of what you are saying. I just have some re-learning ahead of me. Thanks again.

          C 1 Reply Last reply
          0
          • W Wingnut74

            Daniel Turini wrote: Now, it compiles, but does it work? Not always. What if numericThing has a value that is an invalid Thing? You should validate it before assigning. And that's why the C# compiler stopped you. Thanks to both of you. I figured it was something I was missing and was related to the way I have become used to VB's mommy-coddling. I am still a bit out of my comfort zone, but I see the truth of what you are saying. I just have some re-learning ahead of me. Thanks again.

            C Offline
            C Offline
            Cotter
            wrote on last edited by
            #5

            My question is why are you even trying to assign an integer to an enum? Whatever it is be aware of the very useful Enum.Parse For instance, in the case of persisting to fule it might be better to use the enum's name incase the enum's underlyng value was ever changed.

            W 1 Reply Last reply
            0
            • C Cotter

              My question is why are you even trying to assign an integer to an enum? Whatever it is be aware of the very useful Enum.Parse For instance, in the case of persisting to fule it might be better to use the enum's name incase the enum's underlyng value was ever changed.

              W Offline
              W Offline
              Wingnut74
              wrote on last edited by
              #6

              I need to create an object that takes its values from a database. The value of the enum is stored as an int, and I need to convert it back to the enum as I build the object.

              1 Reply Last reply
              0
              • D Daniel Turini

                Wingnut74 wrote: What am I missing? I mean, if it doesn't work this way? What's the point? I do not want to curl into the fetal position and crawl back to VB... As mentioned, you need to cast it to int. And, IMO, this is one of the C# strenghts over VB. Notice, VB allows you to do things that may cause hard to find runtime bugs, while C# will stop them at the moment you're compiling. This is called a "strongly-typed language". notice this code: int numericThing = 1; Things aThing; aThing = (Things)numericThing; Now, it compiles, but does it work? Not always. What if numericThing has a value that is an invalid Thing? You should validate it before assigning. And that's why the C# compiler stopped you. Yes, even I am blogging now!

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                Daniel Turini wrote: What if numericThing has a value that is an invalid Thing? You should validate it before assigning. Or let it be an undefined value :) top secret xacc-ide 0.0.1

                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