473,939 Members | 13,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.val ue2: // No error here
break;
default:
break;
}
}
}

Errors:
-------
Cannot implicitly convert type 'ConsoleApplica tion1.Class1.eT est' to 'uint'
Nov 15 '05 #1
3 7362
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******** ******@TK2MSFTN GP10.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.val ue2: // No error here
break;
default:
break;
}
}
}

Errors:
-------
Cannot implicitly convert type 'ConsoleApplica tion1.Class1.eT est' 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.mi crosoft.com> wrote in message
news:0k******** ******@cpmsftng xa07.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
6263
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 has a virtual Function IsA(f) which tests if the item is member of Family f.
5
22403
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 have the standard. Is this legal? // test.cpp #include <iostream>
2
7432
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
3469
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 static const * int class members as opposed to using enumerations to accomplish a * similar goal. The use of static constants seems more explicit and * obvious to me. Unless I assign values to the enumerators, the * compiler will do it for me....
1
1621
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
3660
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(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
3
11188
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 pass them into the method defined into the base class. Is there a way to do this. E.g. abstract public class BaseWidget {
21
2194
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 enumeration I'll pass. so... I have tried with: string SomeResult (enum** _enum) { }
2
1227
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 are unique for that child class. I can't find a way for a base class to required this. Any ideas? TIA
4
2163
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
10134
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9963
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11110
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11293
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9858
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6077
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.