Getting Error Msg - Cannot implicitly convert type 'string' to 'bool'

后端 未结 5 1995
刺人心
刺人心 2020-12-20 09:16

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\")
         


        
相关标签:
5条回答
  • 2020-12-20 09:36

    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")
    {
    
    0 讨论(0)
  • 2020-12-20 09:36

    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"))
    { 
    }
    
    0 讨论(0)
  • 2020-12-20 09:38

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

    0 讨论(0)
  • 2020-12-20 09:41

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

    0 讨论(0)
  • 2020-12-20 09:42

    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).

    0 讨论(0)
提交回复
热议问题