question about Enum in C#

  • Thread starter genc_ ymeri at hotmail dot com
  • Start date
G

genc_ ymeri at hotmail dot com

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.
 
G

genc_ ymeri at hotmail dot com

I think I got it ........ something like this :

if (Enum.IsDefined(typeof(WaitingTypes),"WAIT_COND_ONLY"))
{

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 !!!")
 
O

Oliver Sturm

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
 
S

sdbillsfan

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.
 
G

genc_ ymeri at hotmail dot com

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;
 
O

Oliver Sturm

genc_ ymeri at hotmail dot com said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top