how to pass Enum

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

    how to pass Enum

    Hi all,

    I am newbie in c# and i have a question.

    I try to work with serial port.
    and i will like to configure the serial port by a form

    I add in a ComboBox.parity some items like:
    None
    Odd
    Even
    Mark
    Space

    then i will like to pass the item value to serialPort.pari ty
    serialPort.pari ty = cmbParity.Selec tedItem
    how can i do ?

    thanks
    Marco
  • Alberto Poblacion

    #2
    Re: how to pass Enum


    "kjqua" <kjqua@pippo.it wrote in message
    news:ue0g7%23gu HHA.5028@TK2MSF TNGP02.phx.gbl. ..
    I add in a ComboBox.parity some items like:
    None
    Odd
    Even
    Mark
    Space
    >
    then i will like to pass the item value to serialPort.pari ty
    serialPort.pari ty = cmbParity.Selec tedItem
    how can i do ?
    You can convert text into enum values using the static method Enum.Parse:

    serialPort.pari ty = Enum.Parse(type of(System.IO.Po rts.Parity),
    cmbParity.Selec tedItem);


    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: how to pass Enum


      "Alberto Poblacion" <earthling-quitaestoparaco ntestar@poblaci on.orgwrote
      in message news:%23ZRiKmhu HHA.4952@TK2MSF TNGP04.phx.gbl. ..
      >
      "kjqua" <kjqua@pippo.it wrote in message
      news:ue0g7%23gu HHA.5028@TK2MSF TNGP02.phx.gbl. ..
      >I add in a ComboBox.parity some items like:
      >None
      >Odd
      >Even
      >Mark
      >Space
      >>
      >then i will like to pass the item value to serialPort.pari ty
      >serialPort.par ity = cmbParity.Selec tedItem
      >how can i do ?
      >
      You can convert text into enum values using the static method Enum.Parse:
      >
      serialPort.pari ty = Enum.Parse(type of(System.IO.Po rts.Parity),
      cmbParity.Selec tedItem);
      Enum.Parse returns object, you still need a cast:

      serialPort.Pari ty =
      (System.IO.Port s.Parity)Enum.P arse(typeof(Sys tem.IO.Ports.Pa rity),
      cmbParity.Selec tedItem);
      >
      >

      Comment

      • kjqua

        #4
        Re: how to pass Enum

        Ben Voigt [C++ MVP] ha scritto:
        >
        "Alberto Poblacion" <earthling-quitaestoparaco ntestar@poblaci on.org>
        wrote in message news:%23ZRiKmhu HHA.4952@TK2MSF TNGP04.phx.gbl. ..
        >>
        >"kjqua" <kjqua@pippo.it wrote in message
        >news:ue0g7%23g uHHA.5028@TK2MS FTNGP02.phx.gbl ...
        >>I add in a ComboBox.parity some items like:
        >>None
        >>Odd
        >>Even
        >>Mark
        >>Space
        >>>
        >>then i will like to pass the item value to serialPort.pari ty
        >>serialPort.pa rity = cmbParity.Selec tedItem
        >>how can i do ?
        >>
        >You can convert text into enum values using the static method Enum.Parse:
        >>
        >serialPort.par ity = Enum.Parse(type of(System.IO.Po rts.Parity),
        >cmbParity.Sele ctedItem);
        >
        Enum.Parse returns object, you still need a cast:
        >
        serialPort.Pari ty =
        (System.IO.Port s.Parity)Enum.P arse(typeof(Sys tem.IO.Ports.Pa rity),
        cmbParity.Selec tedItem);
        >
        Hi,
        I try both code but they don't work
        i have this message error
        The best overloaded method match for 'System.Enum.Pa rse(System.Type ,
        string)' has some invalid arguments
        Argument '2': cannot convert from 'object' to 'string'

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: how to pass Enum

          kjqua <kjqua@pippo.it wrote:
          I try both code but they don't work
          i have this message error
          The best overloaded method match for 'System.Enum.Pa rse(System.Type ,
          string)' has some invalid arguments
          Argument '2': cannot convert from 'object' to 'string'
          So cast cmbParity.Selec tedItem to a string in the call...

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • kjqua

            #6
            Re: how to pass Enum

            Jon Skeet [C# MVP] ha scritto:
            kjqua <kjqua@pippo.it wrote:
            >I try both code but they don't work
            >i have this message error
            >The best overloaded method match for 'System.Enum.Pa rse(System.Type ,
            >string)' has some invalid arguments
            >Argument '2': cannot convert from 'object' to 'string'
            >
            So cast cmbParity.Selec tedItem to a string in the call...
            >
            Thanks now it work
            serialPort.Pari ty =
            (System.IO.Port s.Parity)Enum.P arse(typeof(Sys tem.IO.Ports.Pa rity),
            cmbParity.Selec tedItem.ToStrin g());


            marco

            Comment

            Working...