Taylor
Taylor

Reputation: 13

Operator < cannot be applied to operands of type 'string' and 'int'? What needs to be fixed?

public partial class Form1 : Form   
{ 
    public Form1() 
    { 
        InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
        if (yearworktextBox2.Text < 2 "Rejected")

I am trying to do if statements but I keep getting an error. What is wrong with my code?

Upvotes: 0

Views: 111

Answers (1)

David L
David L

Reputation: 33823

Textbox.Text is a string type and you are comparing it to an integer. You need to parse your textbox value first.

if (int.Parse(yearworktextBox2.Text) < 2 || yearworktextBox2.Text == "Rejected")

However, if your textbox value is not parsable to an integer, it will throw an exception, which seems very likely, since you seem to be expecting either "Rejected" or a numeric value. You can parse it outside of your if statement with a TryParse.

private void button1_Click(object sender, EventArgs e) 
{ 
    int textboxValue;
    int.TryParse(yearworktextBox2.Text, out textboxValue);

    if (textboxValue < 2 || yearworktextBox2.Text == "Rejected")

Upvotes: 2

Related Questions