473,480 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create 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 2683
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**************@TK2MSFTNGP03.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
4780
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...
21
4566
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
3560
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();...
18
11293
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
3383
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...
13
12355
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
3863
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. ...
1
2437
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...
2
2103
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...
34
11125
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...
0
7037
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
6904
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...
1
6732
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
6886
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...
0
5324
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,...
1
4768
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...
0
4472
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...
0
2990
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...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.