Solved

Enumeration????

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS subreport with parameter 3 48
Cant save 3D 4 23
Switching Visual Studio 2015 to normal debug display 3 39
defining NULL or 0 10 44
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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