Looping through enums

W

Wolf

Hi all

Who can Help me with this one?
if I have this Enums:


#region Title
public enum Title: short
{
NA = 0,
Mr = 2,
Adv = 3,
Brig = 4,
Dr = 5,
Genl = 6,
Prof = 7,
Miss = 8,
Mrs = 9,
Adm = 10,
Rev = 11,
Ms = 12,
}


Now I want to take the textbox.text and loop through the enums(Not the
value) to check if it exist in my enums


Something like this:

Public bool DoesTitle Exist
{
blnValid = false
For each enum
{
if (textbox.text == enum.name(Example: "MR"))
return true
break
}

return blnValid ;

}
}


Thanks a mill for those who can help
 
M

Marc Gravell

Well, to do what you want I would actually use Enum.Parse:

Enum.Parse(typeof(Title),theText)

However, to enumerate, just use

Enum.GetNames(typeof(Title))
or
Enum.GetValues(typeof(Title))

Marc
 
M

Marc Gravell

Follow up: it also looks like Enum.IsDefined(typeof(Title), theString) might
do what you want.

Annoyingly, .Net 2 doesn't (as far as I know) provide a type-safe Enum
helper; but if you use enums a lot you might find the following saves a few
casts and "typeof"s; not a massive saving, but can be handy; e.g.

MyEnum[] values = Enum<MyEnum>.GetValues();
or
MyEnum value;
Enum<MyEnum>.TryParse("SomeText", out value);
or
bool defined = Enum<MyEnum>.IsDefined(theString);

etc

Marc

public static class Enum<T> {
public static bool TryParse(string name, out T value) {
return TryParse(name, out value, false);
}
public static bool TryParse(string name, out T value, bool
ignoreCase) {
try {
value = (T) Enum.Parse(typeof(T), name, ignoreCase);
return true;
} catch { // one of the rare cases where this is broadly
acceptable; could test IsDefined first perhaps?
value = default(T);
return false;
}
}
public static T Parse(string name) {
return Parse(name, false);
}
public static T Parse(string name, bool ignoreCase) {
return (T)Enum.Parse(typeof(T), name, ignoreCase);
}
public static string[] GetNames() {
return Enum.GetNames(typeof(T));
}
public static T[] GetValues() {
return (T[])Enum.GetValues(typeof(T));
}
public static string Format(T value, string format) {
return Enum.Format(typeof(T), value, format);
}
public static bool IsDefined(object value) {
return Enum.IsDefined(typeof(T), value);
}
public static Type GetUnderlyingType() {
return Enum.GetUnderlyingType(typeof(T));
}
}
 
N

Nick Hounsome

Wolf said:
Hi all

Who can Help me with this one?
if I have this Enums:


#region Title
public enum Title: short
{
NA = 0,
Mr = 2,
Adv = 3,
Brig = 4,
Dr = 5,
Genl = 6,
Prof = 7,
Miss = 8,
Mrs = 9,
Adm = 10,
Rev = 11,
Ms = 12,
}


Now I want to take the textbox.text and loop through the enums(Not the
value) to check if it exist in my enums


Something like this:

Public bool DoesTitle Exist
{
blnValid = false
For each enum
{
if (textbox.text == enum.name(Example: "MR"))
return true
break
}

return blnValid ;

}
}


Thanks a mill for those who can help

1) See System.Enum.GetNames()

2) A simpler solution with a nicer UI is to populate a combobox with
Enum.GetNames then use Enum.Parse on the selection.

3) I do it all the time but it is neither internationalizable nor easily
extendable: the title should really be a string rather than an enum because
you will (probably) never write a conditional statement based on the value.
 
W

Wolf

Thank you very much for your input, I eventually figure it out and I
must say Enum.Parse is powerfull and nice to use
I just have a last question:

I want to retrieve the ID of _title I send in if it was found:

Like - int EnumID = (int)_title

How do I do this?


_title = "Mr";

Common.Title _myEnum = (Common.Title)Enum.Parse(typeof(Common.Title),
_title, true);

bool pblnIsDefined =
Enum.IsDefined(typeof(Citadel.Horizon.Common.Enums.Common.Title),
_myEnum);
if (pblnIsDefined)
{
contact.Title = currentContact.Title;
contact.PartyStampDate = DateTime.Now;
IOUtility.WriteContact(contact);
break;
}
else
{
string _validhorizonID3 =
currentContact.UserProperties["HorizonID"].Value.ToString();
MessageBox.Show("There is no such title on system, please type a
valid title for Client:" + _validhorizonID3 + ".");

}
 
M

Marc Gravell

What do you mean by ID?

The strongly-typed value (from your bespoke enum), or the index of the
specific enum in the set of enums?

If the former, then you already have it: just cast to Title
If the latter, hmm... I'm not 100% sure that position is guaranteed to be
preserved (vs. the source code), but assuming you have used Enum.GetValues
to list them you could presumably loop through them again without issues;
the following extension to Enum<T> would do the job:

public static int GetIndex(T value) {
int index = 0;
foreach (T item in GetValues()) {
if (Enum.Equals(item,value))
return index;
index++;
}
throw new ArgumentOutOfRangeException();
}

If you are talking about something else (position in a specific drop-down or
similar) then you'd need to use methods appropriate to your implementation.

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Wolf said:
Thank you very much for your input, I eventually figure it out and I
must say Enum.Parse is powerfull and nice to use
I just have a last question:

If I were you I would go with Nick's suggestion and use a combobox instead
of a textbox
 
W

Wolf

I cant go reallly with Nicks suggestion, cause I am using Outlook's
events.

For instance, our system has a set of titles.Our project creates
contacts in outlook.So now when you change the contact's title in
outlook, it updates our system(DB) if the title in outlook exists in
our set titles in our system.

But I figure all out, but of course I couldnt do it without your
help.Thanks alot
 

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

Top