Solved

Enumeration????

Posted on 2006-11-10
4
208 Views
Last Modified: 2010-04-16
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,
0
Comment
Question by:gvector1
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 400 total points
ID: 17918073
Create a class that has properties instead of an enum :

public class FieldNames{

      public static readonly string PatDOB = "dob";

      //etc

}
Then you can use it like an enumeration that has string values  = FieldNames.PatDOB
0
 
LVL 13

Assisted Solution

by:AngryBinary
AngryBinary earned 100 total points
ID: 17918195
If you want to get the same IDE-feel that you get with enumerated values, use constant values in a class (optionally, you can also use a static class):

public class Fields {
    public const string PatDOB = "dob";
    public const string PatName = "name";
}
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17918290
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;
            }

      }
0
 

Author Comment

by:gvector1
ID: 17918314
Excellent solutions. Thank you both.
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question