?
Solved

Enumeration????

Posted on 2006-11-10
4
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 1600 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

801 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