beginner question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • News charter

    #1

    beginner question

    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.F orms.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.



  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: beginner question

    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(mil kQnty.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.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m



    "News charter" <ragannet@chart er.net> wrote in message
    news:vqftoe9egn j220@corp.super news.com...[color=blue]
    > 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.F orms.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.
    >
    >
    >[/color]


    Comment

    • News charter

      #3
      Re: beginner question

      This worked great.

      But I had problem parsing a field with decimal place with Int32.Parse method

      The sale tax field is input 7.5
      when I pass this field to Int32.Parse(sal esTax.Text)

      I get exception.

      Is there another method I can use to parse field with decimal place ?
      Thanks

      Where can I find references to all these methods and what they are used for
      ?
      I guess they are part of the .NET class library but there must be a quick
      references that list all of the
      widely used ones. Is there a book ? or do we learn to use them as we gain
      experience ?

      thanks







      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:uDxkOewoDH A.1656@tk2msftn gp13.phx.gbl...[color=blue]
      > milkQnty is the name of the ComboBox control which has the value in[/color]
      it.[color=blue]
      > To get the currently selected value, you will have to use the Text[/color]
      property[color=blue]
      > to get the text of the selected item. However, this will be a string[/color]
      value,[color=blue]
      > 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[/color]
      you[color=blue]
      > have this, you can use it, like so:
      >
      > // Set the milkPrice.
      > milkPrice = 1.99f;
      >
      > // Get the quantity.
      > int pintMilkQnty = Int32.Parse(mil kQnty.Text);
      >
      > // Get the total.
      > milkTotal = milkPrice * pintMilkQnty;
      >
      > Now, if you were using data binding, you could get a value directly,[/color]
      but[color=blue]
      > I am pretty sure you are not using that. If you are, then I would[/color]
      recommend[color=blue]
      > using the SelectedValue member of the milkQnty combobox in order to get[/color]
      the[color=blue]
      > value associated with the selection.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      >
      >
      > "News charter" <ragannet@chart er.net> wrote in message
      > news:vqftoe9egn j220@corp.super news.com...[color=green]
      > > I'm playing with MS Visual Studio C#
      > >
      > > I have a Combo box that drops down to let me pick a quantity value.[/color][/color]
      called[color=blue][color=green]
      > > 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[/color][/color]
      type[color=blue][color=green]
      > > System.Windos.F orms.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.
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...