473,394 Members | 1,734 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.

question about Enum in C#

well, I making up a "scenario" which may not be the best but I can make my
point at least techincally..,.

I have this enum :

public enum JustTest
{
Volvo = 0x32324234,
Acura = 0x32423443,
Benz = 0x23423434
}

.....and from somewhere (WebService, DB, http or whatever) I get a string
"brandName"of a car name which may have any value e.g "Toyota", "Mazda",
"Benz" or "Volvo". All I want to know how can I check if the that name
exists in my enum list, and if it does I want to get its value as below

public int checkTheList (string brandName)
{

if JustTest.Exist (brandName)
return JustTest.ValueOf(brandName);
else
return -1;
}

Thank You in advance,
G.Y

PS:

Of course I can go and do an if statement for each of them like

if (brandName == JustTest.Volvo.Tostring()
.................................................. ...............

but I think that's not too fency though.... it should be something else to
do it.
Nov 17 '05 #1
5 1329
I think I got it ........ something like this :

if (Enum.IsDefined(typeof(WaitingTypes),"WAIT_COND_ON LY"))
{

WaitingTypes test = (WaitingTypes)
Enum.Parse(typeof(WaitingTypes),"WAIT_COND_ONLY");
switch(test)
{
case WaitingTypes.WAIT_COND_ONLY :
{
button2.Text = "bingo";
}
break;
}

}
else
MessageBox.Show(" this value is not defined !!!")
"genc_ ymeri at hotmail dot com" <ge********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
well, I making up a "scenario" which may not be the best but I can make my
point at least techincally..,.

I have this enum :

public enum JustTest
{
Volvo = 0x32324234,
Acura = 0x32423443,
Benz = 0x23423434
}

....and from somewhere (WebService, DB, http or whatever) I get a string
"brandName"of a car name which may have any value e.g "Toyota", "Mazda",
"Benz" or "Volvo". All I want to know how can I check if the that name
exists in my enum list, and if it does I want to get its value as below

public int checkTheList (string brandName)
{

if JustTest.Exist (brandName)
return JustTest.ValueOf(brandName);
else
return -1;
}

Thank You in advance,
G.Y

PS:

Of course I can go and do an if statement for each of them like

if (brandName == JustTest.Volvo.Tostring()
.................................................. ..............

but I think that's not too fency though.... it should be something else to
do it.

Nov 17 '05 #2
genc_ ymeri at hotmail dot com wrote:

<snip>

Try this:

public int CheckTheList(string brandName) {
try {
return (int) Enum.Parse(typeof(JustTest), brandName);
}
catch (ArgumentException) {
return -1;
}
}

I don't really like the fact that the exception is used as a kind of flow
control instrument here - the other way of doing this would be to use the
GetNames method to retrieve a list of all defined names for the
enumeration and see if the given name is in the list, then use the Parse
method accordingly.

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #3
I usually use structs instead of enums if I need this functionality

ie

public struct Car
{
private string _name;

private Car(string name)
{
_name = name;
}

public static Car Volvo
{
get
{
return new Car("volvo");
}

}

public static Car FromName(string name)
{
switch(name.ToLower())
{
case "volvo":
return Volvo;
}

}

}
This is just a simple example, it's probably better to do it using a
static hashtable similar to the System.Drawing.Color struct.

genc_ ymeri at hotmail dot com wrote:
well, I making up a "scenario" which may not be the best but I can make my
point at least techincally..,.

I have this enum :

public enum JustTest
{
Volvo = 0x32324234,
Acura = 0x32423443,
Benz = 0x23423434
}

....and from somewhere (WebService, DB, http or whatever) I get a string
"brandName"of a car name which may have any value e.g "Toyota", "Mazda",
"Benz" or "Volvo". All I want to know how can I check if the that name
exists in my enum list, and if it does I want to get its value as below

public int checkTheList (string brandName)
{

if JustTest.Exist (brandName)
return JustTest.ValueOf(brandName);
else
return -1;
}

Thank You in advance,
G.Y

PS:

Of course I can go and do an if statement for each of them like

if (brandName == JustTest.Volvo.Tostring()
.................................................. ..............

but I think that's not too fency though.... it should be something else to
do it.


Nov 17 '05 #4
Oliver,
I did something similiar to yours.

Thanks a lot for your help. Very much appreciated !!!!
PS:
I don't really like the fact that the exception is used as a kind of flow
control instrument here - the other way of doing this would be to use the
>>>>

Me neither, so I tried the "IsDefined" method of Enum, and it works so far
.....
if (Enum.IsDefined(typeof(WaitingTypes), brandName))
return Enum.Parse(typeof(JustTest), brandName)
else
return -1;






"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com... genc_ ymeri at hotmail dot com wrote:

<snip>

Try this:

public int CheckTheList(string brandName) {
try {
return (int) Enum.Parse(typeof(JustTest), brandName);
}
catch (ArgumentException) {
return -1;
}
}

I don't really like the fact that the exception is used as a kind of flow
control instrument here - the other way of doing this would be to use the
GetNames method to retrieve a list of all defined names for the
enumeration and see if the given name is in the list, then use the Parse
method accordingly.

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #5
genc_ ymeri at hotmail dot com wrote:
Me neither, so I tried the "IsDefined" method of Enum, and it works so
far ....

if (Enum.IsDefined(typeof(WaitingTypes), brandName))
return Enum.Parse(typeof(JustTest), brandName)
else
return -1;


That's a good idea. Actually the IsDefined method had mutated in my head,
so I seemed to remember it could only take enumeration values, not names :-)
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #6

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

Similar topics

11
by: Vance M. Allen | last post by:
Sorry if the cross-posting wasn't appropriate, but I need help with this and am not sure if it's more appropriate to post under MySQL or Perl...I want to be sure that I can get help from the best...
10
by: James Brown | last post by:
I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING }; (it goes on and on like that) This is what I would like to do: TOKEN t1 = TOK_ID; // ok...
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...
3
by: K. Wilder | last post by:
I need to declare a project level Enum that any procedure in any class can reference so there's continuity with the values. How do I do this? At present, if I declare a Module in a project and...
5
by: =?Utf-8?B?SmVzc2ljYQ==?= | last post by:
Hello, I have a pInvoke question. This is the C function that is exported from one of the C dll, extern __declspec(dllexport) IM_RET_CODE ST_import (IM_MODE mode, char *filename,...
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:
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.