tags:

views:

253

answers:

5

I am checking for values in a textbox to trigger a conditional statement, but am getting error messages.

if (txtAge.Text = "49") || (txtAge.Text = "59")
{
    txtNote.Text = "A valid picture ID must be submitted";
}

The error message I am getting is Cannot implicitly convert type 'string' to 'bool'

How can I resolve this?

+5  A: 

In the if statement replace = by ==.

You're using the assignment operator instead of the equals comparison.

The syntax for the if statement is also not correct. Check if-else (C# Reference).

João Angelo
now I get Invalid expression term '||'
user279521
Remove the closing ) after "49" and the opening ( after ||
Binary Worrier
"59".Equals(txtAge.Text) solves this nicely, also.
lance
Thanks very much for your help.
user279521
@lance: "59".Equals(txtAge.Text) also nicely solves that pesky "easy to visually parse code" problem too. If it's hard to write it should be hard to understand! ROCK ON! :)
Binary Worrier
A: 

You are missing ='s. Also, you may need another set of Brackets around the whole if statement.

Ardman
+1  A: 

you cannot use "=" to compare strings. in this case you could use txtAge.Text.comparedTo("49") == 0 and so on

coolnalu
+1  A: 

When you type this:

if (txtAge.Text = "49")

This basically is assigning "49" to txtAge.Text, and then returning that value as a string (equal to "49").

This is the same, essentially, as doing:

string tmp = txtAge.Text = "49";
if (tmp) { //...

However, you cannot do "if (stringExpression)", since an if statement only works on a boolean. You most likely wanted to type:

if (txtAge.Text == "49" || txtAge.Text == "59")
{
Reed Copsey
user279521
+1  A: 

Need to use == instead of =. Former is used for comparison while the later is for assignment.

A better way is to use Equals method

if (txtAge.Text.Equals("49") || txtAge.Text.Equals("59"))
{ 
}
Asad Butt