if (txtMath.text >= "90")

lblgrad.Text = "A+";
if (txtMath.Text >= "80")
lblgrad.Text = "A";
else if (txtMath.Text >= "70")
lblgrad.Text = "B";
else if (txtMath.Text == "60")
lblgrad.Text = "c";
else if (txtMath.Text == "50")
lblgrad.Text = "D";
else if (txtMath.Text == "50")
MessageBox.Show ("You are failed in your exaim");
It give error as below
//Error 1 Operator '>=' cannot be applied to operands of type //'string' and 'string'

Pleas help me in this matter as soon as possible

Strings can only be compared this way with the != and == operators.
If you want to use greater than or less than, use the string Compare method.

string A = "60";
string B = "80";
int i = A.CompareTo(B);

A < B returns -1
A = B returns 0
A > B returns 1
So in the code snippet above i will be equal to -1, because string A is "smaller" then string B.

Edited 5 Years Ago by ddanbe: n/a

But how i apply this code at my


i have one text box called txtmath and i want use grade.which marks user enter example

user enter 60 in txtmath.text
how can i use it that

if(txtmath >60)
messagebox.show("you have gotten A+ grade);
pleas help me as I requested

Convert strings into integers and then do the comparison:

int intValueA = Convert.ToInt32(txtMath.Text);
if (ibtValueA >= 80)

Edited 5 Years Ago by Mitja Bonca: n/a


You should follow the idea of Mitja, which is the most obvious way to do such things.
But to answer your question you could do a thing like:

i = txtmath.text.CompareTo("80");
This question has already been answered. Start a new discussion instead.