enum <name> : int {} - requires cast to int??

  • Thread starter Thread starter SteveK
  • Start date Start date
S

SteveK

I'm getting the error: "Cannot implicitly convert type
'MovesDBMigrate.MotionNameElementTypes' to 'int'"

for this line of code:
m_nameElementTableNames[MotionNameElementTypes.Character] =
"Tbl_NameCharacters";


Of course if I cast int to it, then it compiles, but if I have declared the
enum's type as int, why do I need to cast int to the value?
Thanks for any help...
 
Steve,

That is right. Enumerations are considered separate types (you don't
need the int in your enumeration declaration, it is that by default) and not
the type that their values are represented in. You have to cast the value
from the enumeration to an int so it can be used as an indexer.

Hope this helps.
 
SteveK said:
I'm getting the error: "Cannot implicitly convert type
'MovesDBMigrate.MotionNameElementTypes' to 'int'"

for this line of code:
m_nameElementTableNames[MotionNameElementTypes.Character] =
"Tbl_NameCharacters";


Of course if I cast int to it, then it compiles, but if I have declared the
enum's type as int, why do I need to cast int to the value?
Thanks for any help...

enum Foo : int {} is just syntactical sugar.
It doesn't declare a true "int" subclass, it
just sets the underlying type for the enum.
Therefore you have to cast, because there is
no implicit convertion between enum and int.

bye
Rob
 
SteveK said:
I'm getting the error: "Cannot implicitly convert type
'MovesDBMigrate.MotionNameElementTypes' to 'int'"

for this line of code:
m_nameElementTableNames[MotionNameElementTypes.Character] =
"Tbl_NameCharacters";


Of course if I cast int to it, then it compiles, but if I have
declared the enum's type as int, why do I need to cast int to the
value? Thanks for any help...

Unfortunately, enums aren't implicitly convertible to anything.
Occassionally, I like this but, most of the time, I hate it.
 
SteveK said:
I'm getting the error: "Cannot implicitly convert type
'MovesDBMigrate.MotionNameElementTypes' to 'int'"

for this line of code:
m_nameElementTableNames[MotionNameElementTypes.Character] =
"Tbl_NameCharacters";


Of course if I cast int to it, then it compiles, but if I have declared the
enum's type as int, why do I need to cast int to the value?
Thanks for any help...

You haven't declared the enum's type as int. You've declared the enum's
*underlying* type as int. Not the same thing. Good job too, as
otherwise enums wouldn't have any type safety.
 
Jon said:
You haven't declared the enum's type as int. You've declared the
enum's underlying type as int. Not the same thing. Good job too, as
otherwise enums wouldn't have any type safety.

IMO, enums are *too* constrained. It would be really useful be able to
add static methods and operator overloads to them. I don't think that's
out of the question considering how they're implemented.

That would allow me to work around the moments that enum type-safety
becomes more of an annoyance than a blessing.
 
Dustin Campbell said:
IMO, enums are *too* constrained. It would be really useful be able to
add static methods and operator overloads to them. I don't think that's
out of the question considering how they're implemented.

That would allow me to work around the moments that enum type-safety
becomes more of an annoyance than a blessing.

Yes, that would be potentially useful. You might like to have a look at
how enums are going to be handled in Java 5 - I think you'd like them
:)
 
thanks all, C++ still sending me down the path of confusion with C#.

Take it easy and thanks again!


Jon Skeet said:
SteveK said:
I'm getting the error: "Cannot implicitly convert type
'MovesDBMigrate.MotionNameElementTypes' to 'int'"

for this line of code:
m_nameElementTableNames[MotionNameElementTypes.Character] =
"Tbl_NameCharacters";


Of course if I cast int to it, then it compiles, but if I have declared the
enum's type as int, why do I need to cast int to the value?
Thanks for any help...

You haven't declared the enum's type as int. You've declared the enum's
*underlying* type as int. Not the same thing. Good job too, as
otherwise enums wouldn't have any type safety.
 
Jon said:
Yes, that would be potentially useful. You might like to have a look
at how enums are going to be handled in Java 5 - I think you'd like
them :)

Of course, since I'm not currently using Java, that just gives me
feature envy. Of course, if it's in Java, I'm sure that Microsoft will
take a serious look the feature.
 
Back
Top