• 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!

Reply to: C# Enum Question

Collapse

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "C# Enum Question"

Collapse

  • palmgeo
    replied
    I didn't have the knowledge of this topic before, now i get some information of C# Enum

    Leave a comment:


  • oliverson
    replied
    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.

    Leave a comment:


  • eek
    replied
    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.

    Leave a comment:


  • DimPrawn
    replied
    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.

    Leave a comment:


  • eek
    replied
    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.

    Leave a comment:


  • Jaws
    replied
    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);
    }

    Leave a comment:


  • Spacecadet
    replied
    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.

    Leave a comment:


  • eek
    replied
    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.

    Leave a comment:


  • Spacecadet
    replied
    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);
                }

    Leave a comment:


  • oliverson
    replied
    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();
    }
    }
    }

    Leave a comment:


  • Spacecadet
    started a topic C# Enum Question

    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"

Working...
X