comboBox2.DataSource = Enum.GetValues(typeof(GSMModemTypeConstants));
GSMModemTypeConstants s_type = (GSMModemTypeConstants)Convert.ChangeType(
comboBox2.Text, typeof(GSMModemTypeConstants));
In the first line the user select the type of his modem from a comboBox which gets the list of the modems available from the Enum GSMModemTypeConstants. In the second line I want to pass the selected modem type to s_type which will later be used by the method that actually sends the SMS.
When I manually set the s_type everything works perfect for example:
GSMModemTypeConstants s_type = GSMModemTypeConstants.gsmModemSonyEricsson;
// OR
GSMModemTypeConstants s_type = GSMModemTypeConstants.gsmModemNokia;
But when I try to retrieve the choice from the comboBox
and set the s_type
like I do in the second line the conversion gives me an error which I can't understand.
Am I handling the Convert.ChangeType
method wrong? If it's not my fault then is there any other way to convert a type from string to something else, other than Convert.ChangeType
?
Solution I still don't have the required reputation to answer it below, so here is the code...
GSMModemTypeConstants s_type = (GSMModemTypeConstants)System.Enum.Parse(
typeof(GSMModemTypeConstants), comboBox2.Text);`
If comboBox2.Text
has a number (integer) then you should first convert that value to int
and then cast it to GSMModemTypeConstants
, eg:
int modemType = int.Parse(comboBox2.Text));
GSMModemTypeConstants s_type = (GSMModemTypeConstants)modemType;
Or, convert comboBox2.SelectedItem
to your enum.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.