string type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all

I need a string type with only three possible values: let's say "A", "B",
"C".
In other words, I need something like enum but of string type:

enum MyStrings {
sA = "A",
sB = "B",
sC = "C"
}

....
MyStrings x;
...
switch(x)
{
case MyStrings.sA:
}


Is there any way to do something like this?

Thank you
Alex
 
Why not use an enum and then cast the enum type to/from a string when
needed?

Or I suppose you could use a string array.
 
Hi all

I need a string type with only three possible values: let's say
"A", "B", "C".
In other words, I need something like enum but of string type:

enum MyStrings {
sA = "A",
sB = "B",
sC = "C"
}

...
MyStrings x;
...
switch(x)
{
case MyStrings.sA:
}


Is there any way to do something like this?

Thank you
Alex

Alex,

Just give the enum members names that reflect what you want in the
string, then use .ToString() to get the string value. If your
string/enum name has spaces, use underscores and remove them when the
enum value is converted to a string:

using System;

namespace Example
{
public enum MyStrings
{
This_is_A,
Another_value_of_B,
ThisHasNoSpaces
}

public class ExampleClass
{
public static void Main()
{
MyStrings ms = MyStrings.Another_value_of_B;
string s = ms.ToString().Replace("_", " ");
Console.WriteLine(s);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top