if number is greater in textbox

c#, if-statement, visual-studio

Solution

`TextBox.Text` property returns a `String` which cannot be compared to a `0.5`, a `double` object. Using your code will throw an error

Error1 Operator '>' cannot be applied to operands of type 'string' and 'double'

Parse the string returned by the text property of your textbox to double before comparing and you will get rid of this error. As Velous said, TryParse is a better choice in case of a format exception.

double no;
bool valid = double.TryParse(txtMoney.Text, out no);
if (valid && no > 0.5) {

       Product cokecollect;
       cokecollect = new Product();

      txtProducts.Text = cokecollect.collectmessage1();
}

Problem

I am new to this so please be patient, I am having a bit of trouble with if statement. I want to print a message in textbox 1 if textbox 2 is over 0.5. Can anyone help please. ``` class Product { public string collectmessage1() { return "Collect your product"; } if (txtMoney.Text > 0.5) { Product cokecollect; cokecollect = new Product(); txtProducts.Text = cokecollect.collectmessage1(); } ```

Original source