Share via


CS0019: Operator '==' cannot be applied to operands of type 'string' and 'int'

Question

Sunday, July 15, 2007 10:30 PM

Hi,

I have a DropDownList named ddlCluster, and I want to do a simle test:

if (ddlCluster.SelectedValue == 0)

{

     //some stuff goes here

}

Now this is returning this error: CS0019: Operator '==' cannot be applied to operands of type 'string' and 'int'

??????

With VB.NET this seems to work. Any thoughts?

All replies (3)

Monday, July 16, 2007 1:40 AM âś…Answered

hi,

I solved it by turning 0 into a string:

if (ddlCluster.SelectedValue == "0")

 

Thanks for your time


Sunday, July 15, 2007 11:06 PM

use ddlCluster.SelectedIndex instead of SelectedValue


Monday, July 16, 2007 7:53 PM

I'd use string.Compare instead.

if (string.Compare(ddlCluster.SelectedValue, "0") != 0)

When I create DropDownLists I will dynamically add a ListItem with a value of "-1". If AutoPostBack is enabled in the handler I will have something like:

if (!ddlCluster.Items.FindByValue("-1").Selected)