469,353 Members | 2,116 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

How to check if a variable is an Enum

Is there a way to check if a certain variable is an enum?

Example code:

public enum MyEnum {
Monday,
Tuesday,
Wednesday
}
public void MyMethod()
{
MyEnum myEnum = MyEnum.Monday;
string myString = "Bla bla bla";
bool firstTry = IsEnum(myEnum);
bool secondTry = IsEnum(myString);
}

Is there a way to write a function IsEnum() so that firstTry is true and
secondTry is false?

myEnum.GetType() tells me that myEnum is of the type MyEnum.
GetTypeCode() tells me that it's an Int32.

Thanks,

-Patrick
Nov 16 '05 #1
10 19617
Patrick,

My guess is that you can check the inheritance chain for the type in
question - it should have System.Enum in the chain.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Patrick B" <ne*******@devzoo.com> wrote in message
news:Op**************@TK2MSFTNGP09.phx.gbl...
Is there a way to check if a certain variable is an enum?

Example code:

public enum MyEnum {
Monday,
Tuesday,
Wednesday
}
public void MyMethod()
{
MyEnum myEnum = MyEnum.Monday;
string myString = "Bla bla bla";
bool firstTry = IsEnum(myEnum);
bool secondTry = IsEnum(myString);
}

Is there a way to write a function IsEnum() so that firstTry is true and
secondTry is false?

myEnum.GetType() tells me that myEnum is of the type MyEnum. GetTypeCode()
tells me that it's an Int32.

Thanks,

-Patrick


Nov 16 '05 #2
Hi Patrick,

Enum.IsDefined(typeof(MyEnum), myValue)

Roland

"Patrick B" schrieb:
Is there a way to check if a certain variable is an enum?

Example code:

public enum MyEnum {
Monday,
Tuesday,
Wednesday
}
public void MyMethod()
{
MyEnum myEnum = MyEnum.Monday;
string myString = "Bla bla bla";
bool firstTry = IsEnum(myEnum);
bool secondTry = IsEnum(myString);
}

Is there a way to write a function IsEnum() so that firstTry is true and
secondTry is false?

myEnum.GetType() tells me that myEnum is of the type MyEnum.
GetTypeCode() tells me that it's an Int32.

Thanks,

-Patrick

Nov 16 '05 #3
Roland,

Is there a way to use IsDefined even if I don't have a myValue to check
against it? I don't want to check if myValue is defined, I want to check
if the Enum itself is defined.

Thanks,

Patrick
Roland Polzer wrote:
Hi Patrick,

Enum.IsDefined(typeof(MyEnum), myValue)

Roland

"Patrick B" schrieb:

Nov 16 '05 #4
Hi Patrick,

maybe I don't see your problem, so let as face the situation

Example code:

public enum MyEnum {
Monday,
Tuesday,
Wednesday
}

public void MyMethod()
{
MyEnum myEnum = MyEnum.Monday;
#
# if MyEnum is never defined, the compiler generates an error! So I assume
MyEnum IS DEFINED!
#
string myString = "Bla bla bla";
bool firstTry = Enum.IsDefined(typeof(MyEnum), myEnum);
# true - cause of myEnum is member of MyEnum

bool secondTry = Enum.IsDefined(typeof(MyEnum), myString);
# false - myString isn't member MyEnum
}

Is it what you want to know?

Roland

"Patrick B" wrote:
Roland,

Is there a way to use IsDefined even if I don't have a myValue to check
against it? I don't want to check if myValue is defined, I want to check
if the Enum itself is defined.

Thanks,

Patrick
Roland Polzer wrote:
Hi Patrick,

Enum.IsDefined(typeof(MyEnum), myValue)

Roland

"Patrick B" schrieb:

Nov 16 '05 #5
Hi,

bool a = false;
try
{
Enum b = (Enum) value_to_check;
a = true;
}
catch{}

Not very elegant, but ... functional :D

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Patrick B" <ne*******@devzoo.com> wrote in message
news:Op**************@TK2MSFTNGP09.phx.gbl...
Is there a way to check if a certain variable is an enum?

Example code:

public enum MyEnum {
Monday,
Tuesday,
Wednesday
}
public void MyMethod()
{
MyEnum myEnum = MyEnum.Monday;
string myString = "Bla bla bla";
bool firstTry = IsEnum(myEnum);
bool secondTry = IsEnum(myString);
}

Is there a way to write a function IsEnum() so that firstTry is true and
secondTry is false?

myEnum.GetType() tells me that myEnum is of the type MyEnum. GetTypeCode()
tells me that it's an Int32.

Thanks,

-Patrick

Nov 16 '05 #6
Aha! Yes, that clears it up.

I knew you could use IsDefined like this:

bool secondTry = Enum.IsDefined(typeof(MyEnum), myString);

But I didn't know you could use it like this:

bool firstTry = Enum.IsDefined(typeof(MyEnum), myEnum);

So it seems that you are telling me that I should be able to write my
IsEnum function like this:

bool IsEnum(object o)
{
return Enum.IsDefined(typeof (o), o);
}
Nov 16 '05 #7
yes, you don't even need a function, just do

bool b = var is Enum;

"Patrick B" wrote:
Aha! Yes, that clears it up.

I knew you could use IsDefined like this:

bool secondTry = Enum.IsDefined(typeof(MyEnum), myString);

But I didn't know you could use it like this:

bool firstTry = Enum.IsDefined(typeof(MyEnum), myEnum);

So it seems that you are telling me that I should be able to write my
IsEnum function like this:

bool IsEnum(object o)
{
return Enum.IsDefined(typeof (o), o);
}

Nov 16 '05 #8
sorry, I meant to reply to your original post.

don't use Enum.IsDefined, it tests for something completely different, and
is not what you want.

"Daniel Jin" wrote:
yes, you don't even need a function, just do

bool b = var is Enum;

"Patrick B" wrote:
Aha! Yes, that clears it up.

I knew you could use IsDefined like this:

bool secondTry = Enum.IsDefined(typeof(MyEnum), myString);

But I didn't know you could use it like this:

bool firstTry = Enum.IsDefined(typeof(MyEnum), myEnum);

So it seems that you are telling me that I should be able to write my
IsEnum function like this:

bool IsEnum(object o)
{
return Enum.IsDefined(typeof (o), o);
}

Nov 16 '05 #9
The IsEnum() method on the Type object should solve your problem.

bool firstTry = myEnum.GetType().IsEnum();
bool secondTry = myString.GetType().IsEnum();

Nov 16 '05 #10
Ahhh... so easy. Thanks.

sadhu wrote:
The IsEnum() method on the Type object should solve your problem.

bool firstTry = myEnum.GetType().IsEnum();
bool secondTry = myString.GetType().IsEnum();

Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by randy1200 | last post: by
21 posts views Thread by Andreas Huber | last post: by
2 posts views Thread by Ray Cassick \(Home\) | last post: by
4 posts views Thread by PokerMan | last post: by
4 posts views Thread by jonpb | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.