Link to home
Start Free TrialLog in
Avatar of gvector1
gvector1

asked on

Enumeration????

I have a situation in which an enumeration would be perfect, except the underlying value needs to be a string instead of a number.  I know there must be a simple solution, but I cannot think of it right off hand.  For example:

public enum Fields{PatName, PatDOB, PatAddress};

But I want the value of Fields.PatName to have a string value of "name" and Fields.PatDOB to have a string value of "dob", etc.  Do I need to use a hashtable for this.  What would be the best way to accomplish this????

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yup, either const or static readonly.
 This works nicely :

      public class FieldNames{

            public const string PatDOB = "dob";
            public const string PatName = "name";
            public const string PatAddress = "address";

            public static object Parse(string FieldName){
                  Type t = typeof(FieldNames);
                  FieldInfo f = t.GetField(FieldName);
                  if (f == null){
                        throw new ArgumentException(FieldName + " is not a valid Field Name.", "FieldName");
                  }
                  object o = f.GetValue(null);
                  return o;
            }

      }
Avatar of gvector1
gvector1

ASKER

Excellent solutions. Thank you both.