user2964786
user2964786

Reputation: 33

Using the Selected Item from Combo box in If Statement

Basically i have a vending machine and the total price will be displayed in a Label. Now there is a combo box showing money for example 1.00 , 2.00 , 5.00 etc.

Now i want to check with an if statement weather the money that is chosen from the combo box is bigger or smaller from the total price.

the code is not working

if (cbMoney.SelectedItem < total)
{
   MessageBox.Show("Not Enough Money");
}

ERROR Description :Operator '<' cannot be applied to operands of type 'object' and 'double'

Upvotes: 1

Views: 1458

Answers (3)

Mike Cheel
Mike Cheel

Reputation: 13106

SelectedItem is an object and will need to be parsed.

if (double.Parse(cbMoney.SelectedItem.ToSTring()) < total)
{
    MessageBox.Show("Not Enough Money");
}

http://msdn.microsoft.com/en-us/library/system.double.parse(v=vs.110).aspx

Upvotes: 2

Nooa
Nooa

Reputation: 34

how do you guys know the total type?

if (Convert.ToDouble(cbMoney.SelectedItem.ToString()) < Convert.ToDouble(total))
{
  MessageBox.Show("Not Enough Money");
}

Upvotes: 2

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

you are taking the SelectedItem object and not converting before checking.

Try as below:

if (Convert.ToDouble(cbMoney.SelectedItem.ToString()) < total)
{
  MessageBox.Show("Not Enough Money");
}

Upvotes: 1

Related Questions