473,788 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enum is int

The documentation for the "is" keyword says:

An is expression evaluates to true if the provided expression is
non-null, and the provided object can be cast to the provided type
without causing an exception to be thrown.

So, given:

enum TestEnum { t1, t2 }
TestEnum te = TestEnum.t1;
int ti = (int)te;

if (te is int)
{
}

Why does the test for (te is int) fail? Is there a work around for a
function like this:

string CStr(Object arg)
{
if (arg is int)
{
}
}

other then always casting enums to ints?

Thanks
Jun 27 '08 #1
4 2700
On second thought, I guess it requires an implicit cast and Enum (BCL)
only defines an explicit one? In terms of a work around for the function:

if (te is Enum)
{
}

works for my purposes.

Sorry for the bandwidth, but I hope this might help someone else.
jonpb wrote:
The documentation for the "is" keyword says:

An is expression evaluates to true if the provided expression is
non-null, and the provided object can be cast to the provided type
without causing an exception to be thrown.

So, given:

enum TestEnum { t1, t2 }
TestEnum te = TestEnum.t1;
int ti = (int)te;

if (te is int)
{
}

Why does the test for (te is int) fail? Is there a work around for a
function like this:

string CStr(Object arg)
{
if (arg is int)
{
}
}

other then always casting enums to ints?

Thanks
Jun 27 '08 #2
On Tue, 29 Apr 2008 16:26:04 -0700, jonpb <no****@nospam. comwrote:
[...]
enum TestEnum { t1, t2 }
TestEnum te = TestEnum.t1;
int ti = (int)te;

if (te is int)
{
}

Why does the test for (te is int) fail?
IMHO, it's always a good idea to read _all_ of the documentation. In
particular:

Note that the is operator only considers reference
conversions, boxing conversions, and unboxing
conversions. Other conversions, such as user-defined
conversions, are not considered.

The type conversion from an enum to an int is not: a reference conversion;
a boxing conversion; or, an unboxing conversion.
Is there a work around for a function like this:

string CStr(Object arg)
{
if (arg is int)
{
}
}

other then always casting enums to ints?
I don't have Visual Studio handy at the moment, but I'm suspicious of the
idea that a boxed enum would successfully be cast to an int. Generally,
boxed values can only be cast to the exact type that they represent. Even
if the types are compatible, attempting to unbox to the wrong type would
usually throw an exception. My recollection is that this applies to
enum-to-int conversions too.

Now, let's assume my recollection is wrong just for the sake of argument..
Assuming you could cast a boxed enum to an int, then I think you could do
at least a couple of different things:

-- The simplest would be to just assume the cast will work. Whether
this is appropriate or not depends on how you expect the code to be used..
But assuming you're not switching execution flow based on various types
here and rather just doing some error checking, you can just define your
method interface such that it requires valid data to be passed. The
exception can be caught by the caller if they aren't sure they are passing
the right data.

-- The next simplest would be to wrap the cast with a try/catch. Of
course, similar to above, this might not be appropriate if you're
potentially going to receive a lot of different types in the normal course
of events, due to the computational cost of handling an exception. But if
exceptions are rare, this would be reasonable.

-- Finally, if the object is an enum, then you might check for Enum
first, cast to that and then use the Enum members to check the base type..
This of course assumes that you can cast a boxed enum to the Enum type,
and I admit that without Visual Studio handy at the moment I'm unable to
verify it. But it's something to try. I'd think if you could cast a
boxed enum to an int, you could probably cast it to the Enum type. :)

Pete
Jun 27 '08 #3
On Tue, 29 Apr 2008 16:38:33 -0700, jonpb <no****@nospam. comwrote:
On second thought, I guess it requires an implicit cast and Enum (BCL)
only defines an explicit one?
That's not actually the problem. For example, casting from Object to an
actual class requires an explicit cast, and the "is" operator will
successfully detect that (assuming the instance is an instance of the
tested class, of course).
In terms of a work around for the function:

if (te is Enum)
{
}

works for my purposes.
Glad to hear you've got a working solution.

Pete
Jun 27 '08 #4
"jonpb" <no****@nospam. comwrote in message
news:uZ******** ******@TK2MSFTN GP03.phx.gbl...
On second thought, I guess it requires an implicit cast and Enum (BCL)
only defines an explicit one? In terms of a work around for the function:

if (te is Enum)
{
}

works for my purposes.

Sorry for the bandwidth, but I hope this might help someone else.
Remember an enum is not always an int.

Michael
Jun 27 '08 #5

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

Similar topics

20
4851
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
21
4607
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
3617
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;
18
11371
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
2
3400
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function and return the name "FirstData"
13
12393
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
10
3907
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. --Randy Yates
1
2461
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and installed the boost library. This is using gcc under FC3.
2
2121
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and installed the boost library. This is using gcc under FC3.
34
11205
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
0
9498
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
10366
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10175
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...
0
8993
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...
1
7518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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
5399
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...
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3675
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.