calculation problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shanehenery
    New Member
    • Feb 2010
    • 24

    calculation problem

    Hi

    It is a calculating form to indicate on how many books you bay and the total discount you will get after you click calculate
    it is not all the coding in the hole form
    just the calculate part
    It is not calculating the 15% discount
    DiscountTextBox
    , discount price
    DiscountedTextB ox
    it only gifs me a 0.00 amount

    ore any of the calculations in the summary group box
    QuantitySumText Box
    DiscountSumText Box
    DiscountedAmoun tSumTextBox
    AverageDiscount TextBox
    is not calculating at all

    Code:
    Private Sub> CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
    'Calculate the price and discount
    Dim QuantityInteger As Integer
    Dim PriceDecimal, ExtendedPriceDecimal, DiscountDecimal, DiscountedPriceDecimal, AverageDiscountDecimal, _
    DISCOUNT_RATE_Decimal, ExtendedPriceDecilmal, QuantitySumInteger, DiscountSumDecimal, SaleCountInteger, _
    DiscountedSumDecimal, AverageDescountDecimal As Decimal
    Try
    'Convert quantity to numeric variable
    QuantityInteger = Integer.Parse(QuantityTextBox.Text)
    Catch ex As Exception
    End Try
    Try
    'Convertprice if quantity was successful.
    PriceDecimal = Decimal.Parse(PriceTextBox.Text)
    'Calculate values for sale.
    ExtendedPriceDecimal = QuantityInteger * PriceDecimal
    DiscountDecimal = Decimal.Round( _
    (ExtendedPriceDecimal * DISCOUNT_RATE_Decimal), 1)
    DiscountedPriceDecimal = ExtendedPriceDecilmal - DiscountDecimal
     
    'Calculate summary values.
    QuantitySumInteger += QuantityInteger
    DiscountSumDecimal += DiscountDecimal
    SaleCountInteger += 1
    AverageDiscountDecimal = DiscountSumDecimal / SaleCountInteger
     
    'Format and display answer for the sale
    ExtendedPriceTextBox.Text = ExtendedPriceDecimal.ToString("C")
    DiscountTextBox.Text = DiscountDecimal.ToString("N")
    DiscountedTextBox.Text = DiscountedPriceDecimal.ToString("C")
    'Format and display summary values.
    QuantitySumTextBox.Text = QuantitySumInteger.ToString()
    DiscountTextBox.Text = DiscountedSumDecimal.ToString("C")
    AverageDiscountTextBox.Text = AverageDescountDecimal.ToString("C")
     
    Catch PriceException As FormatException
    'Handle a price exception
    MessageBox.Show("Price must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    With PriceTextBox
    .Focus()
    .SelectAll()
    End With
    End Try
    Try
    Catch QuantityException As FormatException
    'Handle a quantity exception.
    MessageBox.Show("Quantity must be numeric. ", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    With QuantityTextBox
    .Focus()
    .SelectAll()
    End With
    End Try
    End Sub

    Thanks
    Last edited by tlhintoq; Apr 14 '10, 02:53 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you first created your question you were asked to wrap your code with [code] tags.
    [imgnothumb]http://files.me.com/tlhintoq/10jihf[/imgnothumb]
    It really does help a bunch. Look how much easier it is to read now that someone has done it for you. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      If you put a breakpoint at line 3 and walk through the code line by line, I'll bet it does run... then you get an error... then bail out of the function. Right?

      Comment

      • MrMancunian
        Recognized Expert Contributor
        • Jul 2008
        • 569

        #4
        Did you do what tlhintoq told you to do? I think you just reposted your code without trying...

        Steven

        Comment

        • shanehenery
          New Member
          • Feb 2010
          • 24

          #5
          Hi

          I have put in a breakpoint in line 3
          then when I debug the hole program doesn't work
          I had to end the program

          Comment

          • MrMancunian
            Recognized Expert Contributor
            • Jul 2008
            • 569

            #6
            Any error message?

            Comment

            • shanehenery
              New Member
              • Feb 2010
              • 24

              #7
              Hi
              Once I debug it gives me a message
              ( A first chance exception of type 'System.FormatE xception' occurred in mscorlib.dll )


              then I add my numbers to calculate
              the red dot of the breakpoint gets that yellow arrow on it and the code turns yellow
              after I click calculate then there is a message telling me
              (not Responding)
              then I need to end the program

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                I'm going to assume this is your very first program in Visual Studio.
                Visual Studio is a powerful program. You really should take the time to learn how to use it and understand it. There are many good books on learning Visual Studio *before* you try to write code with it.

                It's kind of like learning to use the controls of a car (gas, brake, steering) before asking for directions on how to drive from New York to Los Angeles.

                the red dot of the breakpoint gets that yellow arrow on it and the code turns yellow
                after I click calculate then there is a message telling me
                (not Responding)
                then I need to end the program
                Your program is 'not responding' because it is stopped on the breakpoint line. While it is stopped you can look at the 'Autos' and 'Locals' pallets to see the values of your variables. You walk through your code one line at a time by pressing the F-10 function key. There are other options as well, listed in the Debug menu, such as F11 to step into a method you are calling.

                Once I debug it gives me a message
                ( A first chance exception of type 'System.FormatE xception' occurred in mscorlib.dll )
                Someplace where you are trying to format one of your numbers is failing.

                You probably also want to start with a smaller amount of code and work up to something as large as what you are doing here. Start with just 1 calculation and 1 text box. Work out what is wrong with the formatting calls you are making. *THEN* you can apply the correct technique to a second calculation, then a third, and so on.

                Comment

                • shanehenery
                  New Member
                  • Feb 2010
                  • 24

                  #9
                  Hi

                  Yes this is my 3rd project
                  I have put in a breakpoint and went from line to line with F10
                  from this line
                  Code:
                   Const DISCOUNT_RATE_Decimal As Decimal = 0.15D
                  and it stops by this line
                  Code:
                   Private Sub RnR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                  once it stoped then my project pops up and it gives me a message in a Immediate window

                  [message] A first chance exception of type 'System.FormatE xception' occurred in mscorlib.dll [/message]

                  wat can be wrong and how do i fix it
                  can you pleas help

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    Code:
                    Const DISCOUNT_RATE_Decimal As Decimal = 0.15D
                    I'm pretty sure you have to specify the 0.15 as a decimal with a lower case d not D

                    Comment

                    • shanehenery
                      New Member
                      • Feb 2010
                      • 24

                      #11
                      Hi

                      It is still not working

                      I cant change the
                      Code:
                      Const DISCOUNT_RATE_Decimal As Decimal = 0.15D
                      0.15D to a lower case d

                      It gos back to a D by defult

                      And I have declared
                      Code:
                      Const DISCOUNT_RATE_Decimal As Decimal = 0.15D
                      as a Decimal

                      Comment

                      • tlhintoq
                        Recognized Expert Specialist
                        • Mar 2008
                        • 3532

                        #12
                        Do you have to supply the 'D' at all?
                        Can't you just say
                        Const DISCOUNT_RATE_D ecimal As Decimal = 0.15

                        I can do that in C#

                        Comment

                        • shanehenery
                          New Member
                          • Feb 2010
                          • 24

                          #13
                          Hi

                          Yes , I can remove the 'D' but it doesn't make to program to calculate
                          it is still not working

                          Comment

                          • mshmyob
                            Recognized Expert Contributor
                            • Jan 2008
                            • 903

                            #14
                            Am I not seeing something here???

                            I don't event see the CONST line that you indicated in your code or your RnR_Load sub code either.

                            Maybe I am missing something. This does not look like the code you are talking about.

                            Correct me if I am wrong.

                            cheers,

                            Comment

                            • shanehenery
                              New Member
                              • Feb 2010
                              • 24

                              #15
                              HI

                              This is the code I am using
                              It needs to calculate the amount of books you bye with its discount of 15%
                              nothing is calculating


                              Code:
                              Public Class Form1
                              
                              'Declare module-level variables and constants
                                  Private QuantitySumInteger, SaleCountinteger As Integer
                                  Private DiscountSumDecimal, DiscountedPriceSumDecimal As Decimal
                                  Const DISCOUNT_RATE_Decimal As Decimal = 0.15D
                              
                                  Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
                                      'Calculate the price and discount
                              
                                      Dim QuantityInteger As Integer
                                      Dim PriceDecimal, ExtendedPriceDecimal, DiscountDecimal, DiscountedPriceDecimal, AverageDiscountDecimal, _
                                       DISCOUNT_RATE_Decimal, ExtendedPriceDecilmal, SaleCountInteger, _
                                         DiscountedSumDecimal, AverageDescountDecimal As Decimal
                                      Try
                                          'Convert quantity to numeric variable
                                          QuantityInteger = Integer.Parse(QuantityTextBox.Text)
                                      Catch ex As Exception
                                      End Try
                              
                              
                              Try
                                          'Convertprice if quantity was successful.
                              
                                          PriceDecimal = Decimal.Parse(PriceTextBox.Text)
                                          'Calculate values for sale.
                                          ExtendedPriceDecimal = QuantityInteger * PriceDecimal
                                          DiscountDecimal = Decimal.Round( _
                                          (ExtendedPriceDecimal * DISCOUNT_RATE_Decimal), 2)
                                          DiscountedPriceDecimal = ExtendedPriceDecilmal - DiscountDecimal
                              
                              
                                          'Calculate summary values.
                                          QuantitySumInteger += QuantityInteger
                                          DiscountSumDecimal += DiscountDecimal
                                          SaleCountInteger += 1
                                          AverageDiscountDecimal = DiscountSumDecimal / SaleCountInteger
                              
                              
                                          'Format and display answer for the sale
                                          ExtendedPriceTextBox.Text = ExtendedPriceDecimal.ToString("C")
                                          DiscountTextBox.Text = DiscountDecimal.ToString("N")
                                          DiscountedPriceTextBox.Text = DiscountedPriceDecimal.ToString("C")
                              
                                          'Format and display summary values.
                              
                                          QuantitySumTextBox.Text = QuantitySumInteger.ToString()
                                          DiscountTextBox.Text = DiscountedSumDecimal.ToString("C")
                                          AverageDiscountTextBox.Text = AverageDescountDecimal.ToString("C")
                              
                              
                                      Catch PriceException As FormatException
                                          'Handle a price exception
                                          MessageBox.Show("Price must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                          With PriceTextBox
                                              .Focus()
                                              .SelectAll()
                                          End With
                                      End Try
                                      Try
                                      Catch QuantityException As FormatException
                                          'Handle a quantity  exception.
                                          MessageBox.Show("Quantity must be numeric. ", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                          With QuantityTextBox
                                              .Focus()
                                              .SelectAll()
                                          End With
                                      End Try
                                  End Sub

                              Comment

                              Working...