473,395 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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



Nov 16 '05 #1
8 8196
SteveK wrote:
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
Nov 16 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"SteveK" <as***@asdfasdfsd.com> wrote in message
news:OG**************@TK2MSFTNGP10.phx.gbl...
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...



Nov 16 '05 #3
SteveK wrote:
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.

--
Best Regards,
Dustin Campbell
Nov 16 '05 #4
SteveK <as***@asdfasdfsd.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Jon Skeet [C# MVP] wrote:
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.

--
Best Regards,
Dustin Campbell
Developer Express, Inc
Nov 16 '05 #6
Dustin Campbell <du*****@give-me-a-break-with-the-spam-devexpress.com>
wrote:
Jon Skeet [C# MVP] wrote:
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.


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
:)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
thanks all, C++ still sending me down the path of confusion with C#.

Take it easy and thanks again!
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
SteveK <as***@asdfasdfsd.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8
Jon Skeet [C# MVP] wrote:
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.

--
Best Regards,
Dustin Campbell
Developer Express, Inc
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christopher | last post by:
I came across this line in a lib I am studing, it simply has class cSceneNode; all by itself. what is that line doing? I am used to seeing a declaration: MyClass test; or definition: class...
0
by: Doug | last post by:
I got this as soon as I installed Vis Studio 2003, which either requires different permissions to inetpub\wwwroot or changed them during installation. I couldn't open an existing solution or...
4
by: Don Wash | last post by:
Hi All! I'm getting the following Error: No DLLs has been compiled yet and nothing in the \bin directory. So it is not the versioning problem or anything like that. And here are the...
3
by: noreponse1 | last post by:
Hello, I have a solution that contains two projects. I have 2 dlls that these projects use. Here's how it's set up: Solution -ProjectA --References ---DLLA -ProjectB
1
by: MAILTONRK | last post by:
Hi, I am a Mainframe guy. I am working with MS access(maintaining a application) for the last 2 weeks. I had one master database and four replicas. One of my replica had trouble in...
0
by: sphinney | last post by:
I have a complex Access 2002 database with multimple tables, queries, forms, and reports. The database is used by miltiple users that have one of four different levels of security. The databae uses...
0
by: Andrzej Lipski | last post by:
Hello I am developing a Windows Ce 5.0 mobile application. I followed the example shown at : Creating Self-Updating Applications With the .NET Compact Framework...
1
by: inteladu | last post by:
I have created an object TF_T.....and I seem to be stuck at this error....please need some suggestions to overcome this.....please....very very urgent!!! thanks!!!
4
by: smadadi | last post by:
When iam trying to save changes to some of the reports i am getting the error message as 'Couldn't save currently locked by user admin on machine <name>' happening with only some of the reports not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.