Spaces in Enum

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVpZ2k=?=

    #1

    Spaces in Enum

    Hi all,
    is it possible to have spaces in enum values?
    I have an enum like this:

    public enum Columns
    { Italy = 1, OtherCountryUE = 2, OtherCountryOCS E = 3, OtherCountr=
    4 }

    and I'd like to have

    public enum Columns
    { Italy = 1, Other Country UE = 2, Other Country OCSE = 3, Other
    Countr= 4 }

    but this form is not valid.

    How can I solve?

    Thanks a lot.
    --
    Luigi

  • Jon Skeet [C# MVP]

    #2
    Re: Spaces in Enum

    On Sep 24, 11:48 am, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
    is it possible to have spaces in enum values?
    No. The names have to be valid C# identifiers. You may wish to provide
    an attribute for each value, if you want a human-readable form.

    Jon

    Comment

    • Hans Kesting

      #3
      Re: Spaces in Enum

      Luigi laid this down on his screen :
      Hi all,
      is it possible to have spaces in enum values?
      I have an enum like this:
      >
      public enum Columns
      { Italy = 1, OtherCountryUE = 2, OtherCountryOCS E = 3, OtherCountr=
      4 }
      >
      and I'd like to have
      >
      public enum Columns
      { Italy = 1, Other Country UE = 2, Other Country OCSE = 3, Other
      Countr= 4 }
      >
      but this form is not valid.
      >
      How can I solve?
      >
      Thanks a lot.
      You can't. The enum-values have to be identifiers, so you are bound by
      the naming rules for identifiers. And that means: no spaces.

      Why do you want it?

      Hans Kesting


      Comment

      • G.S.

        #4
        Re: Spaces in Enum

        On Sep 24, 6:55 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
        On Sep 24, 11:48 am, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
        >
        is it possible to have spaces in enum values?
        >
        No. The names have to be valid C# identifiers. You may wish to provide
        an attribute for each value, if you want a human-readable form.
        >
        Jon
        I assume it's for display purposes (populate drop-down or similar).

        I have done it quick-and-dirty like that:

        //This is my enum
        public enum Status
        {
        Not_selected = 0,
        Pending = 1,
        Complete = 2,
        Re__Notificatio n_Required //NOTE: __ is later replaced with - for
        display purposes; _ is replaced with a space
        }
        //this is a helper that returns a dictionary ~~~: Make itgeneric
        public static Dictionary<int, stringGetCaseFl owStatusSelecti on()
        {
        Dictionary<int, stringret = new Dictionary<int, string>();
        foreach (Status status in Enum.GetValues( typeof(Status)) )
        ret.Add((int)st atus, status.ToString ().Replace("__" ,
        "-").Replace( "_", " "));
        return ret;
        }
        //consum the solution :-)
        this.cmbCurrent Status.DataSour ce = new
        BindingSource(M yClass.GetCaseF lowStatusSelect ion(), null);
        this.cmbCurrent Status.DisplayM ember = "Value";
        this.cmbCurrent Status.ValueMem ber = "Key";


        Jon, if easy, could you post an example of the attribute solution.

        Comment

        • =?Utf-8?B?THVpZ2k=?=

          #5
          Re: Spaces in Enum

          Thanks Jon and Hans.
          The reason is that I have to put these Enum as caption of XtraTreeList of
          DevExpress Suite.

          L

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Spaces in Enum

            On Sep 24, 1:53 pm, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
            Thanks Jon and Hans.
            The reason is that I have to put these Enum as caption of XtraTreeList of
            DevExpress Suite.
            Does XtraTreeList allow you any way of converting from a value to the
            displayed item? (I'd hope so, for internationalis ation purposes if
            nothing else.)

            Jon

            Comment

            • qglyirnyfgfo@mailinator.com

              #7
              Re: Spaces in Enum

              Take a look at the "TypeConver ter" class.

              Or check out some fancy implementation such as




              On Sep 24, 5:48 am, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
              Hi all,
              is it possible to have spaces in enum values?
              I have an enum like this:
              >
              public enum Columns
                      { Italy = 1, OtherCountryUE = 2, OtherCountryOCS E =3, OtherCountr=
              4 }
              >
              and I'd like to have
              >
              public enum Columns
                      { Italy = 1, Other Country UE = 2, Other Country OCSE= 3, Other
              Countr= 4 }
              >
              but this form is not valid.
              >
              How can I solve?
              >
              Thanks a lot.
              --
              Luigi

              Comment

              • Ignacio Machin ( .NET/ C# MVP )

                #8
                Re: Spaces in Enum

                On Sep 24, 6:48 am, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
                Hi all,
                is it possible to have spaces in enum values?
                I have an enum like this:
                >
                public enum Columns
                { Italy = 1, OtherCountryUE = 2, OtherCountryOCS E = 3, OtherCountr=
                4 }
                >
                and I'd like to have
                >
                public enum Columns
                { Italy = 1, Other Country UE = 2, Other Country OCSE = 3, Other
                Countr= 4 }
                >
                but this form is not valid.
                >
                How can I solve?
                >
                Thanks a lot.
                --
                Luigi
                Hi,

                Well, It's pretty obvious why not. What you can do is using a _ but
                IIRC MS guidelines do not recommend this. It does prefer OtherContryUE
                format

                Comment

                • Ignacio Machin ( .NET/ C# MVP )

                  #9
                  Re: Spaces in Enum

                  On Sep 24, 8:53 am, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
                  Thanks Jon and Hans.
                  The reason is that I have to put these Enum as caption of XtraTreeList of
                  DevExpress Suite.
                  >
                  L
                  Hi,

                  You can have a method that transform the enum for you. Not only that
                  but you could want it in different languages for example.

                  Comment

                  • =?Utf-8?B?THVpZ2k=?=

                    #10
                    Re: Spaces in Enum

                    "Jon Skeet [C# MVP]" wrote:
                    Does XtraTreeList allow you any way of converting from a value to the
                    displayed item? (I'd hope so, for internationalis ation purposes if
                    nothing else.)
                    I think so, I have to check on its documentatio.

                    L

                    Comment

                    • Duggi

                      #11
                      Re: Spaces in Enum

                      On Sep 24, 5:53 am, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
                      Thanks Jon and Hans.
                      The reason is that I have to put these Enum as caption of XtraTreeList of
                      DevExpress Suite.
                      >
                      L
                      I think you can achieve with the displayName / tag / value
                      combinations. The values being enumerated, display name for DevExpress
                      Tree item can be set to a different value. And Tag is always there to
                      help. However you need to set tag value to display value.

                      A little tricky solution...

                      - Cnu

                      Comment

                      • Ignacio Machin ( .NET/ C# MVP )

                        #12
                        Re: Spaces in Enum

                        On Sep 24, 10:04 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
                        On Sep 24, 1:53 pm, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
                        >
                        Thanks Jon and Hans.
                        The reason is that I have to put these Enum as caption of XtraTreeList of
                        DevExpress Suite.
                        >
                        Does XtraTreeList allow you any way of converting from a value to the
                        displayed item? (I'd hope so, for internationalis ation purposes if
                        nothing else.)
                        >
                        Jon
                        I'm not so sure for the header text. In some cases you have a
                        InitializeLayou t method that you can use for it. Dont' know for this
                        control though.
                        It can relay in a resource table for globalization

                        Comment

                        Working...