• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

C# Enum Question

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    C# Enum Question

    Quick question about Enums

    I need to convert the "integer" value of an enum to a string value.
    Is there a simple way of doing this

    e.g. if

    Code:
    public enum test {val1, val2, val3}
    
    public string str
    and val2 has been chosen then I want the string value, str, to equal "val2"
    Coffee's for closers

    #2
    Originally posted by Spacecadet View Post
    Quick question about Enums

    I need to convert the "integer" value of an enum to a string value.
    Is there a simple way of doing this

    e.g. if

    Code:
    public enum test {val1, val2, val3}
    
    public string str
    and val2 has been chosen then I want the string value, str, to equal "val2"
    namespace ConsoleApplication1
    {
    public enum Lights
    {
    Red = 10,
    Amber = 20,
    Green = 30
    }

    class Program
    {
    static void Main(string[] args)
    {
    foreach (var light in Enum.GetValues(typeof(Lights)))
    {
    var s = ((int)light).ToString();
    Console.WriteLine(s);
    }

    Console.WriteLine("Press Any Key");
    Console.ReadKey();
    }
    }
    }

    Comment


      #3
      Thanks Oliverson

      Had to change it slightly though as I was just getting the int value:

      Code:
                  foreach (var light in Enum.GetValues(typeof(Lights)))
                  {
                      String s = light.ToString();
                      MessageBox.Show(s);
                  }
      Coffee's for closers

      Comment


        #4
        Originally posted by Spacecadet View Post
        Thanks Oliverson

        Had to change it slightly though as I was just getting the int value:

        Code:
                    foreach (var light in Enum.GetValues(typeof(Lights)))
                    {
                        String s = light.ToString();
                        MessageBox.Show(s);
                    }
        Thats the problem with using var(s) (implicitly typed variables) which personally is lazy programming.

        I really wish Microsoft remembered that the .net framework has always been really good at guessing what a type a variable is up to the point it screws up. And when it screws up it will do it randomly in style.

        Personally all I want to see is code written in such a way that so that it copes with most potential errors. Everytime I see var being used I do wander / how will it blow up in my face.
        merely at clientco for the entertainment

        Comment


          #5
          Originally posted by eek View Post
          Thats the problem with using var(s) (implicitly typed variables) which personally is lazy programming.

          I really wish Microsoft remembered that the .net framework has always been really good at guessing what a type a variable is up to the point it screws up. And when it screws up it will do it randomly in style.

          Personally all I want to see is code written in such a way that so that it copes with most potential errors. Everytime I see var being used I do wander / how will it blow up in my face.
          The example here used var to do something you wouldn't normally do - that is loop through an enum collection. Although the Var type can be replaced with "lights"

          Var and object types have their use in writing generic methods which can take any object or data type as an input.
          Coffee's for closers

          Comment


            #6
            To get the names you just need to use Enum.GetNames(typeof(EnumType)) which returns a string array containing each name.

            eg:

            Code:
            foreach(string name in Enum.GetNames(typeof(Lights))
            {
                  MessageBox.Show(name);
            }

            Comment


              #7
              Originally posted by Spacecadet View Post
              The example here used var to do something you wouldn't normally do - that is loop through an enum collection. Although the Var type can be replaced with "lights"

              Var and object types have their use in writing generic methods which can take any object or data type as an input.
              Which is fine up to the point that something decides to throw a wobbly. I've no problem with generic types for throwing objects around but I do when bob then does an implicit conversion to a particular type without doing any checks beforehand to see if the conversion is plausible or doable.
              Last edited by eek; 29 September 2011, 09:45.
              merely at clientco for the entertainment

              Comment


                #8
                Originally posted by eek View Post
                Which is fine up to the point that something decides to throw a wobbly. I've no problem with generic types for throwing objects around but I do when bob then does an implicit conversion to a particular type without doing any checks beforehand to see if the conversion is plausible or doable.
                Seems pretty clear you don't understand what var means and think it is some type of variant or runtime determined type assignment.

                The type is determined at compile time and not at runtime, hence any dodgy conversion attempts will be picked up at compile time.

                Comment


                  #9
                  Originally posted by DimPrawn View Post
                  Seems pretty clear you don't understand what var means and think it is some type of variant or runtime determined type assignment.

                  The type is determined at compile time and not at runtime, hence any dodgy conversion attempts will be picked up at compile time.
                  Sadly that's theory and not practice. While compile time (and intellisense) will pick up 99% of the issues it doesn't catch all of them especially where multiple interfaces exist.

                  1 recent example is the mongodb c# classes where implicit types are really useful if bob doesn't want to try and understand what the system is trying to do and simple wants to insert some records.

                  It falls apart however when the class library supports multiple interfaces and implicit types have allowed bob to call an interface with a parameter set full of inherent contradictions.

                  Granted its not an issue you may encounter in day to day usage but its blooming annoying when you discover that your data is being randomly corrupted by rogue data.
                  Last edited by eek; 30 September 2011, 11:53.
                  merely at clientco for the entertainment

                  Comment


                    #10
                    Originally posted by eek View Post
                    Sadly that's theory and not practice. While compile time (and intellisense) will pick up 99% of the issues it doesn't catch all of them. 1 prime example is the mongodb c# classes where implicit types are really useful if bob doesn't want to try and understand what the system is trying to do and simple wants to insert some records.

                    It falls apart however when the class library supports multiple interfaces and those implicit types have allowed bob to call an interface with a parameter set with inherent contradictions.

                    Granted its not an issue you may encounter in day to day usage but its blooming annoying when you discover that your data is being randomly corrupted by rogue data.
                    var

                    or something like

                    IEnumerable<ISomething<IDictionary>>


                    (or even worse)

                    The choice is yours.

                    Comment

                    Working...
                    X