Why Does This Work? 
Author Message
 Why Does This Work?

Assuming I have two textboxes, tb1 and tb2, and a button, btn1.

private void btn1_Click (object sender, System.EventArgs e)
{
    tb2.Text = ((MyEnum)Convert.ToInt32(tb1.Text)).ToString();

Quote:
}

private enum MyEnum
{
    Zero = 0,
    One = 1,
    Two = 2,
    Five = 5

Quote:
}

If I put 3 in tb1 then 3 is what appears in tb2. Why? Wouldn't that throw
some sort of exception trying to assign to an enum an invalid option?

Thanks for any insight,

nautonnier



Sat, 20 Nov 2004 03:01:40 GMT  
 Why Does This Work?
One of the strange but true features of C# (covered in my book, by the way
:-).  By definition, an enumeration is defined as equivalent to an integer
type.  Your MyEnum enumeration therefore only specifies shortcuts for some
of the numbers.  Internally the MyEnum type is an integer, so (MyEnum)3
works just as well as (MyEnum)421.

This is true of any enum type, although you can explicitly define the
integer type for an enumerator.  For example

    enum MyOtherEnum : short
    {
        // stuff goes here
    }

Erik
============
Erik Brown
Author of "Windows Forms Programming with C#"
http://www.amazon.com/exec/obidos/ASIN/1930110286


Quote:
> Assuming I have two textboxes, tb1 and tb2, and a button, btn1.

> private void btn1_Click (object sender, System.EventArgs e)
> {
>     tb2.Text = ((MyEnum)Convert.ToInt32(tb1.Text)).ToString();
> }

> private enum MyEnum
> {
>     Zero = 0,
>     One = 1,
>     Two = 2,
>     Five = 5
> }

> If I put 3 in tb1 then 3 is what appears in tb2. Why? Wouldn't that throw
> some sort of exception trying to assign to an enum an invalid option?

> Thanks for any insight,

> nautonnier



Sat, 20 Nov 2004 05:12:03 GMT  
 Why Does This Work?
Thanks Erik. About two minutes before I read your response I found out the
answer and had to implement the following check to prevent any sort of
silliness:

private void btn1_Click (object sender, System.EventArgs e)
{
    MyEnum enumresult = (MyEnum)Convert.ToInt32(tb1.Text);
    if (Enum.IsDefined (typeof (MyEnum), enumresult))
        tb2.Text = enumresult.ToString();
    else
        throw new ArgumentOutOfRangeException;

Quote:
}

Crazy world of programming.


Quote:
> One of the strange but true features of C# (covered in my book, by the way
> :-).  By definition, an enumeration is defined as equivalent to an integer
> type.  Your MyEnum enumeration therefore only specifies shortcuts for some
> of the numbers.  Internally the MyEnum type is an integer, so (MyEnum)3
> works just as well as (MyEnum)421.

> This is true of any enum type, although you can explicitly define the
> integer type for an enumerator.  For example

>     enum MyOtherEnum : short
>     {
>         // stuff goes here
>     }

> Erik
> ============
> Erik Brown
> Author of "Windows Forms Programming with C#"
> http://www.amazon.com/exec/obidos/ASIN/1930110286



> > Assuming I have two textboxes, tb1 and tb2, and a button, btn1.

> > private void btn1_Click (object sender, System.EventArgs e)
> > {
> >     tb2.Text = ((MyEnum)Convert.ToInt32(tb1.Text)).ToString();
> > }

> > private enum MyEnum
> > {
> >     Zero = 0,
> >     One = 1,
> >     Two = 2,
> >     Five = 5
> > }

> > If I put 3 in tb1 then 3 is what appears in tb2. Why? Wouldn't that
throw
> > some sort of exception trying to assign to an enum an invalid option?

> > Thanks for any insight,

> > nautonnier



Sat, 20 Nov 2004 05:42:37 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Why is curses doing this????

2. Why are most HPC done in Fortran?

3. Why are most HPC done

4. Why is it doing this?

5. Why does doing this break my program?

6. C array syntax proposal (was: Why are most HPC done)

7. Done it - Quick C now works

8. Where is the rough work done?

9. Why it works, why it doesn't work ?

10. Why This Works? Why This Not Works?

11. Why VC++ 6.0 progs works slowly than other ?

12. Why does my breakpoint stop working?

 

 
Powered by phpBB® Forum Software