473,387 Members | 1,510 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,387 software developers and data experts.

enum string value

I have string value, and I want to check if enum contains it..

Say I have enum MyTest {
Foo=1,
Bar
}

and is using pass string argument="Foo"; I'd like to check if it in
the enum.

Now I do the following, but I suspect there is an easier way:

foreach (MyTest t in Enum.GetValues(typeof(MyTest )))
{
if (argument== t.ToString())
{
//DO SOMETHING
}
}

Thanks
Oct 17 '08 #1
3 1857
Well, you could use Enum.Parse(), or Enum.IsDefined() - although note
that it has been suggested that Enum.IsDefined() should be avoided. I
haven't made up my mind on the subject yet... but see here:
http://blogs.msdn.com/kcwalina/archi...18/134208.aspx

Marc

Oct 17 '08 #2
"Marc Gravell" <ma**********@gmail.comwrote in message
news:2b**********************************@k37g2000 hsf.googlegroups.com...
http://blogs.msdn.com/kcwalina/archi...18/134208.aspx
I shudder to think what the color "Ultraviolate" looks like....
Oct 17 '08 #3
I shudder to think what the color "Ultraviolate" looks like....

lol - sounds like a title from the "somewhat dubious" section of the
video store...

Marc
Oct 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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 {
10
by: Ken Allen | last post by:
The ToString() function, when applied to a variable that is an enumeration type, results in a string that is the name of the enumerated value that was defined in the source code. This is cool, but...
3
by: cyshao | last post by:
Hi ,my friends: I have a enum type ,EMyEnum , defigned as . I just want to do something like this: foreach( EMyEnum crrEnum in EMyEnum ) { //do something }
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...
6
by: Jason Larion | last post by:
When working with enums, I've noticed some behaviour that seems completely counter-intuitive to me. I was wondering if someone here could help restore my sanity, or at least help me to understand...
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
8
by: tony | last post by:
Hello! I have below a for loop and a switch in the for loop. I have also a enum called colBlowStep with some values. I have also an array called m_columnBlowStep with some strings. All items in...
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...
6
by: Rico | last post by:
Hello, I'm looking for a way to reference the string name of an enumerator. I know in VB.net you can do "MyEnum.ToString" and get the name instead of the integer value. Is there a way I can do...
3
by: shapper | last post by:
Hello, I have an enum: Public Enum Color Red Blue Green End Enum
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.