beginner question

  • Thread starter Thread starter News charter
  • Start date Start date
N

News charter

I'm playing with MS Visual Studio C#

I have a Combo box that drops down to let me pick a quantity value. called
milkQnty

However, my understanding is that C# treats this as an object.
I want to use this value to multiply with the price to get the total.

double milkPrice, milkTotal;
milkPrice = 1.99f;
milkTotal = milkPrice * milkQnty;

When I compile, I get a message that says I cannot apply operands of type
System.Windos.Forms.ComboBox; and 'double'

How can I turn the value in object milkQnty into an integer I can do
arithmetic with ?

Trying to learn C# and this is all new to me.

Thanks in advance.
 
milkQnty is the name of the ComboBox control which has the value in it.
To get the currently selected value, you will have to use the Text property
to get the text of the selected item. However, this will be a string value,
and you need a number. To convert it to a number, pass that value to the
static Parse method on the Int32 class (which is the same as int). Once you
have this, you can use it, like so:

// Set the milkPrice.
milkPrice = 1.99f;

// Get the quantity.
int pintMilkQnty = Int32.Parse(milkQnty.Text);

// Get the total.
milkTotal = milkPrice * pintMilkQnty;

Now, if you were using data binding, you could get a value directly, but
I am pretty sure you are not using that. If you are, then I would recommend
using the SelectedValue member of the milkQnty combobox in order to get the
value associated with the selection.

Hope this helps.
 
Back
Top