why enum members need conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nikhil Patel

    #1

    why enum members need conversion

    Hi all,
    I am a VB6 programmer and learning C#. I am currently reading a chapter
    on types. I have question regarding enums. Why do we need to convert enum
    members to the value that they represent?

    Thanks in advance...

    -Nikhil


  • Nikhil Patel

    #2
    Re: why enum members need conversion

    Thanks for the reply.
    I think I didnt give more details on my question. Let me give an
    example:

    public enum Numbers
    {
    zero=0,
    one=1,
    two=2
    }

    now if I want to use Numbers.two in mathematical calculations, I need to
    explicitly convert it to int as follows,

    MessageBox.Show (((int)Numbers. two * 2).ToString()); //Displays 4

    But if we use the Numbers enum in switch statement, we dont have to convert
    it.

    So my question is why do we have to convert the members in some cases.
    VB.Net does not require the implicit conversion of the enum numbers even if
    you use them in mathematical calculations.

    Thanks...

    -Nikhil

    "Dmitri Shvetsov" <dshvetsov@cox. net> wrote in message
    news:F%ESa.7423 $Ne.7231@fed1re ad03...[color=blue]
    > It can be very helpful when you send these enums through interfaces for
    > example and these enum types are unknown in another objects.
    > Then if you use integer values for example in pairs to these enums you can
    > convert these values to integers and back, use in switch statements,
    > somewhere else. In 2 words - it's more flexible.
    >
    > My example. I use these enums to define some errors in method parameters[/color]
    and[color=blue]
    > can send these eror codes through the COM+ interface if I need. Then I can
    > use a switch based method to get an error string for any defined enum
    > values.
    >
    > public enum Parameter {
    > E_Invalid = 1000,
    > E_IsNull = 1001,
    > E_IsEmpty = 1002,
    > E_NothingToDo = 1003
    > };
    >
    > It depends what really do you need. Sometimes you can use an array of[/color]
    enums[color=blue]
    > without integer analogs and this will also work.
    >
    > Regards,
    > Dmitri.
    >
    > "Nikhil Patel" <nikhil0100@aol .com> wrote in message
    > news:%23uGDZxvT DHA.3188@tk2msf tngp13.phx.gbl. ..[color=green]
    > > Hi all,
    > > I am a VB6 programmer and learning C#. I am currently reading a[/color]
    > chapter[color=green]
    > > on types. I have question regarding enums. Why do we need to convert[/color][/color]
    enum[color=blue][color=green]
    > > members to the value that they represent?
    > >
    > > Thanks in advance...
    > >
    > > -Nikhil
    > >
    > >[/color]
    >
    >[/color]


    Comment

    • rubber21

      #3
      Re: why enum members need conversion

      why? why?

      SECURITY man. More RULES. RED TAPE. Or incapacitation if you look closer.
      Don't enter the MATRIX! Don't lose your freedom! Stay with VB or better jump
      to C++ if you want to know what FREESTYLE is.

      open your eyes!

      "Nikhil Patel" <nikhil0100@aol .com> wrote in message
      news:#uGDZxvTDH A.3188@tk2msftn gp13.phx.gbl...[color=blue]
      > Hi all,
      > I am a VB6 programmer and learning C#. I am currently reading a[/color]
      chapter[color=blue]
      > on types. I have question regarding enums. Why do we need to convert enum
      > members to the value that they represent?
      >
      > Thanks in advance...
      >
      > -Nikhil
      >
      >[/color]



      Comment

      • Jon Skeet

        #4
        Re: why enum members need conversion

        [If this post comes through twice, but with a different last sentence,
        take this one as the more correct one :) ]

        Nikhil Patel <nikhil0100@aol .com> wrote:[color=blue]
        > I am a VB6 programmer and learning C#. I am currently reading a chapter
        > on types. I have question regarding enums. Why do we need to convert enum
        > members to the value that they represent?[/color]

        Type safety, basically. You have to prove to the compiler that you
        really know that the variable in question is an enum and not an int,
        but that in this case you want to treat it as an int, or vice versa.

        Sometimes it gets in the way, other times it's useful.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: why enum members need conversion

          Nikhil,
          Learn to use Option Strict On in VB.NET! :-)

          With option strict on VB.NET does require the explicit conversion of enums,
          to perform math. The only 'math' operator defined for Enum types in VB.NET
          is the Or operator to combine Enums that have the Flags attribute applied
          for example. Of course you can compare two Enums and assign an Enum also.

          You should normally use Option Strict On at the top of each of your VB.NET
          files, to ensure compile time errors over run time errors. As Jon Skeet
          stated, type safety!

          Hope this helps
          Jay

          "Nikhil Patel" <nikhil0100@aol .com> wrote in message
          news:OU3iGwxTDH A.1552@TK2MSFTN GP10.phx.gbl...[color=blue]
          > error in last post.
          >
          > Please read this line...
          > I said VB.Net does not require the implicit conversion of the enum
          > numbers even if you use them in mathematical calculations. Please read it
          > as, VB.Net does not require the explicit conversion of the enum numbers[/color]
          even[color=blue]
          > if you use them in mathematical calculations.
          >
          > Sorry about that.
          >
          > "Nikhil Patel" <nikhil0100@aol .com> wrote in message
          > news:u92aeoxTDH A.2180@TK2MSFTN GP10.phx.gbl...[color=green]
          > > Thanks for the reply.
          > > I think I didnt give more details on my question. Let me give an
          > > example:
          > >
          > > public enum Numbers
          > > {
          > > zero=0,
          > > one=1,
          > > two=2
          > > }
          > >
          > > now if I want to use Numbers.two in mathematical calculations, I need to
          > > explicitly convert it to int as follows,
          > >
          > > MessageBox.Show (((int)Numbers. two * 2).ToString()); //Displays 4
          > >
          > > But if we use the Numbers enum in switch statement, we dont have to[/color]
          > convert[color=green]
          > > it.
          > >
          > > So my question is why do we have to convert the members in some cases.
          > > VB.Net does not require the implicit conversion of the enum numbers even[/color]
          > if[color=green]
          > > you use them in mathematical calculations.
          > >
          > > Thanks...
          > >
          > > -Nikhil
          > >
          > > "Dmitri Shvetsov" <dshvetsov@cox. net> wrote in message
          > > news:F%ESa.7423 $Ne.7231@fed1re ad03...[color=darkred]
          > > > It can be very helpful when you send these enums through interfaces[/color][/color][/color]
          for[color=blue][color=green][color=darkred]
          > > > example and these enum types are unknown in another objects.
          > > > Then if you use integer values for example in pairs to these enums you[/color][/color]
          > can[color=green][color=darkred]
          > > > convert these values to integers and back, use in switch statements,
          > > > somewhere else. In 2 words - it's more flexible.
          > > >
          > > > My example. I use these enums to define some errors in method[/color][/color][/color]
          parameters[color=blue][color=green]
          > > and[color=darkred]
          > > > can send these eror codes through the COM+ interface if I need. Then I[/color][/color]
          > can[color=green][color=darkred]
          > > > use a switch based method to get an error string for any defined enum
          > > > values.
          > > >
          > > > public enum Parameter {
          > > > E_Invalid = 1000,
          > > > E_IsNull = 1001,
          > > > E_IsEmpty = 1002,
          > > > E_NothingToDo = 1003
          > > > };
          > > >
          > > > It depends what really do you need. Sometimes you can use an array of[/color]
          > > enums[color=darkred]
          > > > without integer analogs and this will also work.
          > > >
          > > > Regards,
          > > > Dmitri.
          > > >
          > > > "Nikhil Patel" <nikhil0100@aol .com> wrote in message
          > > > news:%23uGDZxvT DHA.3188@tk2msf tngp13.phx.gbl. ..
          > > > > Hi all,
          > > > > I am a VB6 programmer and learning C#. I am currently reading a
          > > > chapter
          > > > > on types. I have question regarding enums. Why do we need to convert[/color]
          > > enum[color=darkred]
          > > > > members to the value that they represent?
          > > > >
          > > > > Thanks in advance...
          > > > >
          > > > > -Nikhil
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...