String enumeration/grouping

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • archway

    #1

    String enumeration/grouping

    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.En umItem1

    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


  • Guest's Avatar

    #2
    Re: String enumeration/grouping

    http://www.codeguru.com/cs_syntax/StringToEnum.html


    "archway" <archway_NOSPAM @hotmail.com> wrote in message
    news:eGuQb9BXDH A.2212@TK2MSFTN GP09.phx.gbl...[color=blue]
    > 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.En umItem1
    >
    > 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[/color]
    together[color=blue]
    > in a similar manner to an enumeration. Has anyone got any ideas on how to
    > achieve this?
    >
    > Thanks in advance
    >
    >[/color]


    Comment

    • archway

      #3
      Re: String enumeration/grouping

      Thanks for the link, but how would this work for strings that contain
      spaces?


      <.> wrote in message news:##Gzw$BXDH A.1748@TK2MSFTN GP12.phx.gbl...[color=blue]
      > http://www.codeguru.com/cs_syntax/StringToEnum.html
      >
      >
      > "archway" <archway_NOSPAM @hotmail.com> wrote in message
      > news:eGuQb9BXDH A.2212@TK2MSFTN GP09.phx.gbl...[color=green]
      > > 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[/color][/color]
      would[color=blue][color=green]
      > > 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[/color][/color]
      a[color=blue][color=green]
      > > class whereby you wish to set a default value.
      > >
      > > e.g.
      > >
      > > private myStringEnum _myEnumProperty = myStringEnum.En umItem1
      > >
      > > 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[/color]
      > together[color=green]
      > > in a similar manner to an enumeration. Has anyone got any ideas on how[/color][/color]
      to[color=blue][color=green]
      > > achieve this?
      > >
      > > Thanks in advance
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Guest's Avatar

        #4
        Re: String enumeration/grouping

        http://tinyurl.com/bv05

        Here is another one.

        "archway" <archway_NOSPAM @hotmail.com> wrote in message
        news:uC#EFNCXDH A.384@TK2MSFTNG P12.phx.gbl...[color=blue]
        > 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
        >
        > <.> wrote in message news:eRW48KCXDH A.2484@TK2MSFTN GP09.phx.gbl...[color=green]
        > > Why dont you try it and come back to us :D
        > >
        > > I found that on google, amazing what google contains.Google has all the
        > > answers.
        > >
        > > "archway" <archway_NOSPAM @hotmail.com> wrote in message
        > > news:OXiMnICXDH A.3268@tk2msftn gp13.phx.gbl...[color=darkred]
        > > > Thanks for the link, but how would this work for strings that contain
        > > > spaces?
        > > >
        > > >
        > > > <.> wrote in message news:##Gzw$BXDH A.1748@TK2MSFTN GP12.phx.gbl...
        > > > > http://www.codeguru.com/cs_syntax/StringToEnum.html
        > > > >
        > > > >
        > > > > "archway" <archway_NOSPAM @hotmail.com> wrote in message
        > > > > news:eGuQb9BXDH A.2212@TK2MSFTN GP09.phx.gbl...
        > > > > > 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[/color][/color][/color]
        that[color=blue][color=green][color=darkred]
        > > > 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[/color][/color][/color]
        properties[color=blue][color=green]
        > > in[color=darkred]
        > > > a
        > > > > > class whereby you wish to set a default value.
        > > > > >
        > > > > > e.g.
        > > > > >
        > > > > > private myStringEnum _myEnumProperty = myStringEnum.En umItem1
        > > > > >
        > > > > > This would throw an exception, as the value is a string, but you[/color][/color][/color]
        are[color=blue][color=green][color=darkred]
        > > > > > declaring it of myStringEnum type.
        > > > > >
        > > > > > Basically, I would like to be able to create a way to group[/color][/color][/color]
        strings[color=blue][color=green][color=darkred]
        > > > > together
        > > > > > in a similar manner to an enumeration. Has anyone got any ideas on[/color][/color]
        > how[color=green][color=darkred]
        > > > to
        > > > > > achieve this?
        > > > > >
        > > > > > Thanks in advance
        > > > > >
        > > > > >
        > > > >
        > > > >
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • archway

          #5
          Re: String enumeration/grouping

          Yeah, well I can see your searching technique is just fine for your tastes,
          but not really up my street.

          <.> wrote in message news:OqNYIPCXDH A.2204@TK2MSFTN GP12.phx.gbl...[color=blue]
          > http://tinyurl.com/bv05
          >
          > Here is another one.
          >
          > "archway" <archway_NOSPAM @hotmail.com> wrote in message
          > news:uC#EFNCXDH A.384@TK2MSFTNG P12.phx.gbl...[color=green]
          > > Yes, thanks for the sarcasim, I had looked on google prior to posting[/color][/color]
          the[color=blue][color=green]
          > > 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
          > >
          > > <.> wrote in message news:eRW48KCXDH A.2484@TK2MSFTN GP09.phx.gbl...[color=darkred]
          > > > Why dont you try it and come back to us :D
          > > >
          > > > I found that on google, amazing what google contains.Google has all[/color][/color][/color]
          the[color=blue][color=green][color=darkred]
          > > > answers.
          > > >
          > > > "archway" <archway_NOSPAM @hotmail.com> wrote in message
          > > > news:OXiMnICXDH A.3268@tk2msftn gp13.phx.gbl...
          > > > > Thanks for the link, but how would this work for strings that[/color][/color][/color]
          contain[color=blue][color=green][color=darkred]
          > > > > spaces?
          > > > >
          > > > >
          > > > > <.> wrote in message news:##Gzw$BXDH A.1748@TK2MSFTN GP12.phx.gbl...
          > > > > > http://www.codeguru.com/cs_syntax/StringToEnum.html
          > > > > >
          > > > > >
          > > > > > "archway" <archway_NOSPAM @hotmail.com> wrote in message
          > > > > > news:eGuQb9BXDH A.2212@TK2MSFTN GP09.phx.gbl...
          > > > > > > 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[/color][/color]
          > that[color=green][color=darkred]
          > > > > 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[/color][/color]
          > properties[color=green][color=darkred]
          > > > in
          > > > > a
          > > > > > > class whereby you wish to set a default value.
          > > > > > >
          > > > > > > e.g.
          > > > > > >
          > > > > > > private myStringEnum _myEnumProperty = myStringEnum.En umItem1
          > > > > > >
          > > > > > > This would throw an exception, as the value is a string, but you[/color][/color]
          > are[color=green][color=darkred]
          > > > > > > declaring it of myStringEnum type.
          > > > > > >
          > > > > > > Basically, I would like to be able to create a way to group[/color][/color]
          > strings[color=green][color=darkred]
          > > > > > together
          > > > > > > in a similar manner to an enumeration. Has anyone got any ideas[/color][/color][/color]
          on[color=blue][color=green]
          > > how[color=darkred]
          > > > > to
          > > > > > > achieve this?
          > > > > > >
          > > > > > > Thanks in advance
          > > > > > >
          > > > > > >
          > > > > >
          > > > > >
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • archway

            #6
            Re: String enumeration/grouping

            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

            "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
            news:Oe7UYSCXDH A.2576@TK2MSFTN GP09.phx.gbl...[color=blue]
            > As I can see, you are using consts, so it is not dynamically generated.[/color]
            So,[color=blue]
            > why you don't create a normal enum {Value1, Value2 ...}
            > You can always get their string representation with .ToString(), and you[/color]
            can[color=blue]
            > always use a string value to reference a member with Enum.Parse ...
            >
            > Sunny
            > "archway" <archway_NOSPAM @hotmail.com> wrote in message
            > news:eGuQb9BXDH A.2212@TK2MSFTN GP09.phx.gbl...[color=green]
            > > 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[/color][/color]
            would[color=blue][color=green]
            > > 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[/color][/color]
            a[color=blue][color=green]
            > > class whereby you wish to set a default value.
            > >
            > > e.g.
            > >
            > > private myStringEnum _myEnumProperty = myStringEnum.En umItem1
            > >
            > > 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[/color]
            > together[color=green]
            > > in a similar manner to an enumeration. Has anyone got any ideas on how[/color][/color]
            to[color=blue][color=green]
            > > achieve this?
            > >
            > > Thanks in advance
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Jon

              #7
              Re: String enumeration/grouping

              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;
              }
              }


              "archway" <archway_NOSPAM @hotmail.com> wrote in message news:eGuQb9BXDH A.2212@TK2MSFTN GP09.phx.gbl...[color=blue]
              > 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.En umItem1
              >
              > 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
              >
              >[/color]


              Comment

              • archway

                #8
                Re: String enumeration/grouping

                Do you have any idea how I could implement some form of looping within this
                structure? For example, use a foreach ?

                Thanks

                "Michael Mayer" <mrmayer@charte r.net> wrote in message
                news:OkE22jTXDH A.1900@TK2MSFTN GP10.phx.gbl...[color=blue]
                > What about this? -mike
                >
                > // The typesafe enum pattern adapted from
                > //
                > http://developer.java.sun.com/develo...ava/page1.html
                > public sealed class Suit
                > {
                > private String name;
                >
                > private Suit(String name) { this.name = name; }
                >
                > public override String ToString() { return name; }
                >
                > public static Suit Clubs =
                > new Suit("clubs with spaces");
                > public static Suit Diamonds =
                > new Suit("diamonds" );
                > public static Suit Hearts =
                > new Suit("hearts");
                > public static Suit Spades =
                > new Suit("spades");
                > }
                >
                > class tester
                > {
                > static void Main()
                > {
                > Suit suit1 = Suit.Clubs;
                > Suit suit2 = Suit.Hearts;
                > Suit suit3 = Suit.Spades;
                > Console.WriteLi ne(suit1.ToStri ng());
                > Console.WriteLi ne(suit2.ToStri ng());
                > Console.WriteLi ne(suit3.ToStri ng());
                > Console.ReadLin e();
                >
                > }
                > }
                >
                >
                > "archway" <archway_NOSPAM @hotmail.com> wrote in message
                > news:eGuQb9BXDH A.2212@TK2MSFTN GP09.phx.gbl...[color=green]
                > > However, I was wondering whether you had ever created something that[/color]
                > would[color=green]
                > > 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[/color]
                > in a[color=green]
                > > class whereby you wish to set a default value.
                > >
                > > e.g.
                > >
                > > private myStringEnum _myEnumProperty = myStringEnum.En umItem1
                > >
                > > 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[/color]
                > together[color=green]
                > > in a similar manner to an enumeration. Has anyone got any ideas on[/color]
                > how to[color=green]
                > > achieve this?
                > >
                > > Thanks in advance
                > >
                > >[/color]
                >
                >[/color]


                Comment

                Working...