473,394 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 19947
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: randy1200 | last post by:
The following enum is given to me, and I can't change it: enum yo { ONE, TWO, THREE }; I have the following: char test = "ONE"; Any ideas on how to see if the string in "test" is in the...
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 {
2
by: Ray Cassick \(Home\) | last post by:
I have a function that takes a value is as System.Enum and I want to be able to look at that value and determine if it is a regular enum or an enum that has a <Flag()> attribute set on it. I am...
2
by: JasonC | last post by:
Hi, First time posting here so please be gentle! I wish to check a variable for a number of words that are in a array. Not sure if this is the best way to do it, but this is what i have. ...
4
by: PokerMan | last post by:
Hi guys, Maybe someone can explain thisi have this enum: public enum LimitType : int { BottomLimit, TopLimit, Limit }
3
by: maxjackie | last post by:
i have code below i want use $user and $password which LoginId and password in the html code please hel me <?php session_start(); require_once('DB.php'); function validate() { $user =...
29
by: javaalien | last post by:
Kindly please anyone who knows is the any method to know what is the limit of length of a counter? Depsite from initiliazed variable's types (either int, byte, long etc) is there any way for me...
1
by: Bhagyashreepazare | last post by:
hi.... i am very new to javascript and asp programming ...i wrote a one function for calclulation of fields but i am not getting the write value...there must be some wrong in function...i just want...
17
by: wswilson | last post by:
In python, I could write: a = 1 if a in : do something... In c (and many other languages):
4
by: jonpb | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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,...
0
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...

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.