Enum.Parse doesn't trigger an exception (C# 2.0)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cadilhac@gmail.com

    Enum.Parse doesn't trigger an exception (C# 2.0)

    Hi,

    I have the following code:

    public enum MyColors { Red, Green, Blue }
    MyColors c = (MyColors)Enum. Parse(typeof(My Colors), "abcd");

    Why do I get c = "abcd" after this code instead of getting a
    ArgumentExcepti on ?
    MSDN says that this expetion is triggered when value is a name, but not
    one of the named constants defined for the enumeration.
    Thank you

    Herve

  • cadilhac@gmail.com

    #2
    Re: Enum.Parse doesn't trigger an exception (C# 2.0)

    oops, my example is not correct. When the value is "abcd" it triggers
    the right exception.
    The problem is when the value is an integer string like "1234". In this
    case, no exception is triggered and my enum now equals 1234.
    Thank you

    Herve

    Comment

    • Brian Brown

      #3
      Re: Enum.Parse doesn't trigger an exception (C# 2.0)

      Hi,

      When you pass the value (2nd) argument to Enum.Parse it will see if it can
      match or convert the value that is passed. If the value is a empty string,
      only has white space, or is a string but not one of the named constants
      defined in the enumeration it will throw an ArgumentExcepti on. If it can
      convert the value to the Enum’s underlying type it will create a new instance
      of the enumeration with that value. For instance:

      //with this enum
      public enum Color { Red, Green, Blue }

      //creates a new instance of Color with the value of 1 (Green)
      Color c = (Color)Enum.Par se(typeof(Color ), "1");
      Console.WriteLi ne(c.ToString(" G"));

      //create a new instance of Color with the value of 3 (no name)
      Color d = (Color)Enum.Par se(typeof(Color ), "3");
      Console.WriteLi ne(d.ToString(" G"));

      //create a new instance of Color with the value of 2 (Blue)
      Color e = (Color)Enum.Par se(typeof(Color ), "Blue");
      Console.WriteLi ne(e.ToString(" G"));

      //Throw an ArgumentExcepti on because there is no match for the value
      //and it cannot convert the value to the underlying type
      Color f = (Color)Enum.Par se(typeof(Color ), "Yellow");
      Console.WriteLi ne(f.ToString(" G"));


      From the documentation I believe that this method has not changed from 1.1
      to 2.0. If anyone knows any different please reply to this post.

      I hope this helps
      -----------------------


      Comment

      • cadilhac@gmail.com

        #4
        Re: Enum.Parse doesn't trigger an exception (C# 2.0)

        Your example is correct but you don't try to set a string integer like
        "1234". As I said, it doesn't trigger an exception and I get my enum
        variable with the value 1234.
        Is it considered normal behaviour ?

        Herve

        Comment

        • Brian Brown

          #5
          Re: Enum.Parse doesn't trigger an exception (C# 2.0)

          Herve,

          I would say yes this is normal behavior. In my example I used 3 but you can
          substitute 1234 or any string that will convert to the underlying type of the
          enum and the result will be the same. As long as it can be converted it will
          not throw an exception.

          Hope this helps.
          --------------------

          Comment

          • cadilhac@gmail.com

            #6
            Re: Enum.Parse doesn't trigger an exception (C# 2.0)

            So what can I do to ensure that the value assigned is in the Range of
            the enum ? I must test that on an enum I receive in parameter in a
            method but I don't know what Type it is exactly (i.e. I know this is an
            enum but I don't know this is a MyColors).

            Thank you

            Herve

            Comment

            • Brian Brown

              #7
              Re: Enum.Parse doesn't trigger an exception (C# 2.0)

              Herve,

              You can use either Enum.IsDefined( typeof(<enum>), value) which will return a
              true or a false if the value falls within the range of the enum (best) or you
              can use Enum.GetUnderly ingType(typeof( <enum>)) and see if the value that was
              passed can be converted to that type.

              I am not sure that I understand what you mean by you do not know what the
              enum type is when it is passed to a method. If neither one of these methods
              help can you post some code that I may look at? Maybe it will help me
              understand your question better.

              Hope this helps.
              --------------------------

              Comment

              • cadilhac@gmail.com

                #8
                Re: Enum.Parse doesn't trigger an exception (C# 2.0)

                Thanks a lot Brian.
                IsDefined works perfectly for my need.

                May I ask something out of this topic ? While investigating for this
                problem, I tried to output strings in the Visual C# 2005 beta console
                window but nothing appears. I use for example:

                System.Diagnost ics.Debug.Write Line("test");
                Thank you

                Herve

                Comment

                • Brian Brown

                  #9
                  Re: Enum.Parse doesn't trigger an exception (C# 2.0)

                  Herve,

                  Great! I am glad that it worked for you. As for your question about the
                  console window I would suggest that you go to the vs 2005 newsgroups and do a
                  search. I am sure that you will find the answer to that there.

                  Good Luck.
                  ----------------------

                  --VS 2005 news groups


                  Comment

                  Working...