By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,081 Members | 862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,081 IT Pros & Developers. It's quick & easy.

enum is int

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.