Re: Trying to understand enums



On Tue, 23 Jun 2009 16:12:01 -0700, AMP <ampeloso@xxxxxxxxx> wrote:

[...]
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
.



Relevant Pages

  • Re: Newbie Question
    ... It may be a problem but the compiler error clearly stated that he tried to ... declare a method where only a class, delegate, enum, interface, or struct is ... >> public static extern int SendCharFmtMsg(IntPtr hWnd, int Msg, int ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trying to understand enums
    ... that's the name of the enum value. ... They are a way to declare new, ... constant types as an int. ... integer base type, it's true that you can do a lot of integer-like things   ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Enumerated types
    ... Enum ACGroup ... I cannot then access the members of the group. ... One alternative I could try, in the mean time, is to declare a string array ...
    (microsoft.public.access.formscoding)
  • Re: non-static method gotcha
    ... the declared enum, 'SubmissionSite' in your example. ... Its effect is to declare that C is not an inner class. ... extends the immediately enclosing enum type. ... decompiled anonymous class. ...
    (comp.lang.java.help)
  • Re: stratgies for organizing elements in a passed array
    ... enumeration is nothing except a special syntax to declare a set of named ... You don't need f2003 to declare a set of named constants. ... to have symbolic names for the various keywords. ... I use an old preprocessor to get the equivalent of an enum. ...
    (comp.lang.fortran)

Loading