String enumeration/grouping

  • Thread starter Thread starter archway
  • Start date Start date
A

archway

I know you cannot have string enumerations such as:

enum myStringEnum {
enumItem1 = "value 1",
enumItem2 = "value 2",
etc
}

However, I was wondering whether you had ever created something that would
mimic this? I have tried the following:

struct myStringEnum {
public const string EnumItem1 = "value 1";
public const string EnumItem2 = "value 2";
etc
}

However, the problem with this is when you wish to define properties in a
class whereby you wish to set a default value.

e.g.

private myStringEnum _myEnumProperty = myStringEnum.EnumItem1

This would throw an exception, as the value is a string, but you are
declaring it of myStringEnum type.

Basically, I would like to be able to create a way to group strings together
in a similar manner to an enumeration. Has anyone got any ideas on how to
achieve this?

Thanks in advance
 
Thanks for the link, but how would this work for strings that contain
spaces?
 
http://tinyurl.com/bv05

Here is another one.

archway said:
Yes, thanks for the sarcasim, I had looked on google prior to posting the
message and could not find a satisfactory answer. Perhaps my searching
technique is just pants!! :-)

I will have a play with it, but am not convinced on first impressions.

Thanks
 
Yeah, well I can see your searching technique is just fine for your tastes,
but not really up my street.
 
Yes, I knew about using ToString(). I forgot to mention that I would need
the string that is returned to not necessarily be the same as the name of
the enum item.

For example:

enum {
myEnumItem1 = "My Enum Item 1",
myEnumItem2 = "Something completely different"
}

Although unlikely there will be anything completely different, it is more of
a case of being able to have spaces really.

Thanks
 
Do you need a particular ordinal associated with the string?

If not, you can use:

public class StringGroup{
public static readonly string Str1 = "string #1";
public static readonly string Str2 = "string #2";
}

else you can use something like:

public struct StringEnum{
public static readonly StringEnum Str1 = new StringEnum(0);
public static readonly StringEnum Str2 = new StringEnum(1);

private static string[] strings = new string[2] {
"string #1",
"string #2",
}

private int val;

public StringEnum( int val ) {
this.val = val;
}

public override string ToString() {
return strings( value );
}

public static explicit operator int ( StringEnum stringEnum ) {
return stringEnum.val;
}
}
 
Do you have any idea how I could implement some form of looping within this
structure? For example, use a foreach ?

Thanks
 

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

Enumeration and Class 4
Get Strings 3
Enumerator 2
Enum Extentions 7
use any enumerator as function parameter 3
generic Mapper to Enumerations 1
about enum 3
Enumeration string representations 1

Back
Top