473,398 Members | 2,113 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,398 software developers and data experts.

Enum base-type question

The MSDN help on the enum keyword for the C# language indicates that it is
possible to give an enumeration a base-type other than int. In my case, I
have chosen to give it a base type of uint.

The annoying thing I have encountered, is that when I switch on a uint
value, I still have to explicitly cast my enum values to uint - even though
they should be uint values. Is this by design (I don't know why because my
enum is uint and so is the switch value), or is there something I have
missed that would remove the required explicit uint cast?

Thanks, Tyler

Code sample:
------------
class Class1
{
protected enum eTest : uint
{
value1,
value2
};

[STAThread]
static void Main(string[] args)
{
uint tvuiValue = 1;
switch( tvuiValue )
{
case eTest.value1: // ERROR here
break;
case (uint)eTest.value2: // No error here
break;
default:
break;
}
}
}

Errors:
-------
Cannot implicitly convert type 'ConsoleApplication1.Class1.eTest' to 'uint'
Nov 15 '05 #1
3 7334
I don't believe you have missed anything. I don't believe there is anything
that would allow you to avoid casting.
Regards,
Mountain

"Tyler" <ty***@work.com> wrote in message
news:OQ**************@TK2MSFTNGP10.phx.gbl...
The MSDN help on the enum keyword for the C# language indicates that it is
possible to give an enumeration a base-type other than int. In my case, I
have chosen to give it a base type of uint.

The annoying thing I have encountered, is that when I switch on a uint
value, I still have to explicitly cast my enum values to uint - even though they should be uint values. Is this by design (I don't know why because my enum is uint and so is the switch value), or is there something I have
missed that would remove the required explicit uint cast?

Thanks, Tyler

Code sample:
------------
class Class1
{
protected enum eTest : uint
{
value1,
value2
};

[STAThread]
static void Main(string[] args)
{
uint tvuiValue = 1;
switch( tvuiValue )
{
case eTest.value1: // ERROR here
break;
case (uint)eTest.value2: // No error here
break;
default:
break;
}
}
}

Errors:
-------
Cannot implicitly convert type 'ConsoleApplication1.Class1.eTest' to 'uint'

Nov 15 '05 #2

Hi Tyler,

Thanks for posting in this group.
C# is a strong type language, it is strict on types.
The swith case state in C# does not apply the inheritance comparasion, but
apply equal comparasion.
So you should explicit cast them into one type.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #3
That helps to clarify that I'm not missing something and that the explicit
cast is required in this case.

Thanks, Tyler

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:0k**************@cpmsftngxa07.phx.gbl...

Hi Tyler,

Thanks for posting in this group.
C# is a strong type language, it is strict on types.
The swith case state in C# does not apply the inheritance comparasion, but
apply equal comparasion.
So you should explicit cast them into one type.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #4

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

Similar topics

5
by: Ernst Murnleitner | last post by:
Hello readers, I want to enumerate all values in an enum, e.g.: { enum Family{A=0, B,C, D = 100, E}; BaseItem * p = Factory::Get...// BaseItem is the basis of many other items // BaseItem...
5
by: Nils Petter Vaskinn | last post by:
I'm using an enum that's declared within a function (since I only need it within that function.) I can't find anything about this in "The C++ Programming Language" by Stroustroup and I don't...
2
by: Nobody | last post by:
Hi All, In my application I have the following implementation for ClassA and ClassC invoking a call on ClassB. void ClassA::DoSomething() { int value = m_pClassB->Read(enum1); }
12
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using...
1
by: | last post by:
Would be nice to specify an incremenet or decremenet as an attribute in an enum so I can for example have the following... enum SomeEnum { A = 0, B, // 2 C, // 4 D /6
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
3
by: ssg31415926 | last post by:
I have an abstract base class with an enum which is passed into a method. I want my derived classes to be able to add new values to the enum without having to redefine it and I want to be able to...
21
by: dllhell | last post by:
hi all, I have a problem with creating proc with a enum as a param. I wish to pass an enumeration in proc in which one I intend to do some operations with enumeration, but I don't know which one...
2
by: RJ | last post by:
I was hoping to code a base class that would somehow require inheriting classes to define an Enum. Each inheriting child class needs to define it's own enum with it's own enumerated values, which...
4
by: Jure Bogataj | last post by:
Hi all! I have two enumerations: public enum MyEnum1 and public enum MyEnum2 I want to create function that accepts parameter as member of either enumeration. Is this at all possible?
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.