Trying to understand enums

A

AMP

Hello,
When I use a Constant:
const int MyAge = 200;
Console.WriteLine(MyAge)
Produces:
200

But when I use :
enum Me

{

MyAge=200
}

Console.WriteLine(Me.MyAge);

I get
MyAge.

I want to use an enum as constant in the same fashion, but I must be
misunderstaning Enums???
Any Help?
Thanks
Mike
 
A

AMP

[...]
But when I use :
enum Me
MyAge=200
}
Console.WriteLine(Me.MyAge);

I get
MyAge.

Sure, that's the name of the enum value.
I want to use an enum as constant in the same fashion, but I must be
misunderstaning Enums???

Yes.  Enums aren't supposed to be a way to declare integer constants.  
They are a way to declare new, simple value types.  It gives you a modicum  
of type-safety for things that would otherwise be represented as some  
integer base type.  If you want a constant typed as an int, then declare a  
constant types as an int.

Because they are little more than a thin, type-safe veneer on top of an  
integer base type, it's true that you can do a lot of integer-like things 
with them, and of course can even cast to and from integer types.  But  
that doesn't mean that you can expect to use them exactly like you'd use  
an integer.  They are still a completely different type, with semantically  
non-integer, named values as their actual values.

Pete

Pete,
Thanks.
Since they are allowed in Namespaces, I was hoping I could use them as
a global variable to represent some constant numbers I'm using.
What would you do?
Thanks
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top