Convert string to System.IO.Ports.Parity C #


I have a constructor to connect to the serial port of the type:

PortConx = new SerialPort(puerto);

And I define its parameters in the following way through a textbox:

PortConx.Parity = Parity.Even;                         
PortConx.StopBits = StopBits.Two;                     
PortConx.DataBits = Convert.ToInt32(textBoxDB.Text);

The fact is that, for parity, I can not pass it a string type variable because the data it asks for is of type System.IO.Ports.Parity .

How could I convert it to that type of data?

asked by Wilfred 30.12.2016 в 03:38

1 answer


Use the Enum.Parse method, by which you can convert any string to the value of the equivalent enum. Here is an example:

string parity = "Even";
PortConx.Parity = (Parity)Enum.Parse(typeof(Parity), parity, true);
answered by 30.12.2016 / 08:56