Solved

Enumeration????

Posted on 2006-11-10
4
207 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent solutions. Thank you both.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now