C# Noob in trouble with enumerations
-
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... -
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...You need to cast it to Things: Things aThing = (Things)numericThing;
-
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...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!
-
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!
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.
-
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.
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.
-
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.
-
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!
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