472,985 Members | 2,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 software developers and data experts.

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 2652
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
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
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
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
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
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
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
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
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
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
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.