Thursday, January 29, 2015

How do I write a proper if statement like so..

Firstly, in c# you can't use English-style syntax like "if something equals A or B". You have to explicitly spell out each comparison, as in "if something equals A or something equals B".


Secondly, to specify a string literal, you must enclose it in quotes.


So, if t2g1.SelectedItem is a string in a ComboBox, the statement should read something like the following



if((t2g1.SelectedItem.ToString() == "A") || (t2g1.SelectedItem.ToString() == "A+"))
{
// etc.
}

Note that even though SelectedItem is already a string, the ToString is required because it is returned from the ComboBox as an object.

No comments:

Post a Comment