Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Type like Enum?

Status
Not open for further replies.

robertfah

Programmer
Joined
Mar 20, 2006
Messages
380
Location
US
Is there a way to create a String type that I can access in code to reference a string instead of a number like the Enum?
 
I'm not sure I understand. Is this what you mean?
Code:
    Enum MyValues
        Value1
        Value2
        Value3
    End Enum
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim x As New MyValues
        x = MyValues.Value1
    End Sub


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Sort of...kinda like this:
Code:
    public string CustomString
    {
        string1="this",
        string2="that",
        string3="other"
    }

Then in code, I could reference it just like enums, but now it's strings:

Code:
switch(CustomString)
{
      case CustomString.string1:
           do something
      case CustomString.string2;
           dp something
}

Does that make more sense?
 
Oh I know I can't do it with the Enum type, but can I do it another way, somehow?
 
why must it be a string? it's simply an identitier, so integers are just as useful. Plus it's an enum so you can use the name which can read like words
Code:
public enum MyOptions : 1
{
   Option1,
   Option2,
   Option3
}

MyOptions op = //get option
switch(op)
{
   case MyOptions.Option1:
      break;
   case MyOptions.Option2:
      break;
   case MyOptions.Option3:
      break;
}
if you want a pretty string identitfier to go with it, use a custom enum helper like this
Code:
public static class MyOptionsHelper
{
   private static IDictionary<MyOptions, string> prettyStrings;

   private static void LoadStrings()
   {
      if(prettyStrings == null)
      {
          prettyStrings = new Dictionary<MyOptions, string>();
          prettyStrings.Add(MyOptions.Option1, "Foo");
          prettyStrings.Add(MyOptions.Option1, "Bar");
          prettyStrings.Add(MyOptions.Option1, "Foo Bar");
      }
   }

   public static string GetPrettyString(MyOptions op)
   { 
      LoadStrings();
      return prettyStrings[op];
   }

   public static string GetPrettyString(int op)
   { 
       return GetPrettyString((MyOptions)op);
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top