Reputation: 91
In the comboBox_SelectedIndexChanged when I use
if (comboBox2.SelectedItem == "1")
{
MessageBoxEx.Show("Ok1");
}
if (comboBox2.SelectedItem == "2")
{
MessageBoxEx.Show("Ok2");
}
if (comboBox2.SelectedItem == "3")
{
MessageBoxEx.Show("Ok3");
}
if (comboBox2.SelectedItem == "4")
{
MessageBoxEx.Show("Ok4");
}
I get the warning "Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'" and the message boxes do not pop up when that value is clicked. What needs to be done to fix this?
Upvotes: 2
Views: 3136
Reputation: 9862
You can use MessageBox.Show(comboBox2.SelectedItem.ToString());
because SelectedItem
is an object type datatype. So you have to convert it into string
Upvotes: 0
Reputation: 216263
Just add a ToString() to the SelectedItem property
if (comboBox2.SelectedItem.ToString() == "1")
SelectedItem property is typed as Object and thus you cannot compare against a string without an explict conversion to string.
But I should ask, how do you fill the Items collection? Are the items of string type?
Also do not assume that the SelectedIndexChanged is raised only when there is an item selected.
Add a check before trying to access the SelectedItem property like this
if(combobox2.SelectedItem != null)
{
// Start your checks on the selectedItem
if (comboBox2.SelectedItem.ToString() == "1")
{
MessageBoxEx.Show("Ok1");
}
..... and so on....
}
Upvotes: 3
Reputation: 67898
So let's rewrite this a little because the problem is SelectedItem
is an object
and you're comparing a string
. How about something like this:
var val = Convert.ToString(comboBox2.SelectedItem);
switch (val)
{
case "1":
break;
case "2":
break;
default:
break;
}
this will also allow you handle the default
case where you get back an empty string if the SelectedItem
is null
. You don't want to assume that there will always be a value in SelectedItem
. Further, if the SelectedItem
is null
, the approach I provided won't throw an exception. However, this statement comboBox2.SelectedItem.ToString()
will throw a NullReferenceException
.
OK, so based on your comment, which deviates from the code example, you have two ways of handling OR conditions. The first is leveraging the fall through of the switch
like this:
switch (val)
{
case "1":
case "2":
break;
default:
break;
}
in that example both 1
and 2
will fall into the same code line. However, if you need even more robust branching you'll have to use if
statements -but now you won't be getting any warnings and you won't have to worry about a NullReferenceException
either.
Upvotes: 3
Reputation: 3967
you are trying to compare an object to a string ("3"). The compilator warn you because the .toString() might not return what you expect. It warn you so you can do the appropriate action to be sure to have a good comparaison.
In this case, as almost everyone stated, juste simply use SelectedItem.ToString()
Upvotes: 0
Reputation: 6132
SelectedItem
is an Object. This object contains a String
property though.
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem.aspx
Upvotes: 0