471,353 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

string type

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
Feb 23 '06 #1
3 1494
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.

Feb 23 '06 #2
Why does it have to be a string?

Feb 23 '06 #3
"=?Utf-8?B?QWxleCBLLg==?=" <Al***@discussions.microsoft.com> wrote
in news:B3**********************************@microsof t.com:
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);
}
}
}

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Feb 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Derek Hart | last post: by
1 post views Thread by Neil Schemenauer | last post: by
16 posts views Thread by Don Starr | last post: by
7 posts views Thread by al | last post: by
5 posts views Thread by th3dude | last post: by
reply views Thread by =?Utf-8?B?TGFzdGJ1aWxkZXJz?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.