Solved

Enumeration????

Posted on 2006-11-10
4
211 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 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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