calculation problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #16
    Your entire process is in a single Try/Catch block. That means if any single part fails, the entire things fails. You should break this up so that each individual calculation is in it's own try/catch.

    [message] A first chance exception of type 'System.FormatE xception' occurred in mscorlib.dll [/message]
    You know you have a format exception. So stop trying to integrate formating in your calls.
    Code:
    DiscountTextBox.Text = DiscountedSumDecimal.ToString("C")
    Take out all your "C" and "N" formatters. Just throw whatever string is made into the textbox to at least see if your calculations are working. *AFTER IT ALL CALCULATING* then you can format just one line and run it again. If that works, format one more line.

    I have put in a breakpoint and went from line to line with F10
    from this line
    Expand|Select|W rap|Line Numbers
    Const DISCOUNT_RATE_D ecimal As Decimal = 0.15D
    If you are only using F10 then you are skipping over the actual calculation part, for line-by-line stepping.

    Since you want to debug the method that takes place after you click the [Calculate] button, that is where you should put the breakpoint: Inside the Calculate button handler.
    Code:
    Dim QuantityInteger As Integer
    Line 11 from your sample above.
    Now you can step through the method causing trouble until you find the bug.

    Comment

    • shanehenery
      New Member
      • Feb 2010
      • 24

      #17
      Hi

      I have added the individual Try/Catch block of each calculation
      and I have removed the "C"and"N"
      it is still not calculating at all

      only thing it is calculating is the "QuantityTextBo x"+ "PriceTextB ox" ="ExtendedPrice TextBox"

      Comment

      • mshmyob
        Recognized Expert Contributor
        • Jan 2008
        • 903

        #18
        I am a little confused again.

        I now see where you are declaring DISCOUNT_RATE_D ecimal (line 6) as a constant, but then later under your button code you dimension it again (line 13). I have never done that but usually when you dimension a variable it resets the value of the variable to the default value (which would be 0 in this case).

        Could it be that by dimensioning your variable AFTER you have set it as a constant that it is no longer a constant and is proceeding through your code with a value of 0 instead of .15 ?

        cheers,

        Comment

        • shanehenery
          New Member
          • Feb 2010
          • 24

          #19
          Hi

          ok I left DISCOUNT_RATE_D ecimal as a Const
          and deleted the one that was extra in dimensioning(li ne 13)
          it is calculating the dscount as 0.15D thats fine now
          but it is showing "0.00" in the DiscountTextBox
          when I click calculate.

          but the rest of the calculation is not working
          be cos from ther I need to calculate the summary af the Total Discount , Total Discount Amount and Average Discount

          Comment

          • shanehenery
            New Member
            • Feb 2010
            • 24

            #20
            Hi

            and it is giving me this message "A first chance exception of type 'System.DivideB yZeroException' occurred in mscorlib.dll" and "A first chance exception of type 'System.FormatE xception' occurred in mscorlib.dll"

            where can I look for the code so then I can fix it

            Comment

            • mshmyob
              Recognized Expert Contributor
              • Jan 2008
              • 903

              #21
              Originally posted by shanehenery
              Hi

              ok I left DISCOUNT_RATE_D ecimal as a Const
              and deleted the one that was extra in dimensioning(li ne 13)
              it is calculating the dscount as 0.15D thats fine now
              but it is showing "0.00" in the DiscountTextBox
              when I click calculate.

              but the rest of the calculation is not working
              be cos from ther I need to calculate the summary af the Total Discount , Total Discount Amount and Average Discount
              OK you are still confusing me with your responses.

              Fixing your dimensioning fixed your discount problem.

              Now you say the DiscountTextBox displays 0.00 - does this mean DiscountedSumDe cimal gives the proper answer but is not being displayed on your form properly?

              What does it mean when you say the rest of the calculation is not working?

              Maybe the other people are mind readers but I failed that course.

              Can you please be very very specific and indicate what the variables are when the system gives the error.

              Also as previously indicated remove ALL error trapping and ALL formatting and work with just the bare bones code needed to get the result you want. After that is working you can throw back in formatting and trapping.

              cheers,

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #22
                Here's what I'm hearing at this point...
                "The entire thing doesn't work - some one tell me what's wrong with it."

                You need to debug your program one line at a time. You need to use the tools Visual Studio provides. The 'Locals' and 'Autos' pallets will show you the values of your variables at each line.

                Do a calculation, step one line
                Do a calculation, step one line
                As soon as the answer to one calculation is wrong - stop debugging and fix that one calculation.
                Then start over
                Do a calculation, step one line
                Do a calculation, step one line

                Eventually you will fix each calculation so they all work.

                This is how basic debugging works. One line at a time. It's slow and tedieous: Welcome to programming!

                This is a very simple need. It is nothing more complex than 7th grade math. Don't make more of it than it really is. It's easy to get frustrated and see the entire program as one big problem. Don't get caught in that trap. Break it down into little pieces. One line at a time. Don't try to solve the entire application. Just solve one line. Then solve another line.

                Comment

                • barnaba
                  New Member
                  • Apr 2010
                  • 8

                  #23
                  have you found the solution?

                  Comment

                  • shanehenery
                    New Member
                    • Feb 2010
                    • 24

                    #24
                    Discount Deduction Calculation Problem

                    Hi

                    I have a problem in my coding I will like to deduct 15% from my RentalAmountTex tBox to be displayed in my
                    AmountDueTextBo x

                    Code:
                    AmountDueTextBox.Text = RentalAmountDecimal - 0.1
                    then in my AmountDueTextBo x it only displays -0.1
                    it is not doing the calculation
                    an you pleas help

                    Code:
                    Public Class VideoBonanza
                        'Declare module-level variables and constants.
                    
                        Private TotalSumInteger, MemberInteger, SaleCountInteger As Integer
                        Private TotalRentalSumDecimal As Decimal
                        Const Discount_Rate_Decimal As Decimal = 0.1D
                    
                        Private Sub VideoBonanza_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                            
                        End Sub
                    
                        Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
                            'Calculate the rental amounts 
                    
                            Dim QuantityInteger As Integer
                            Dim RentalAmountDecimal, DiscountDecimal, AmountDueDecimal As Decimal
                    
                            Try
                                'Convert Quantity to numeric variable.
                                QuantityInteger = Integer.Parse(QuantityTextBox.Text)
                                AmountDueTextBox.Text = (RentalAmountDecimal - DiscountDecimal).ToString("C")
                                DiscountDecimal = Decimal.Round(RentalAmountDecimal * Discount_Rate_Decimal, 2)
                                AmountDueDecimal = RentalAmountDecimal - DiscountDecimal
                                RentalAmountTextBox.Text = RentalAmountDecimal.ToString("C")
                                AmountDueTextBox.Text = AmountDueDecimal.ToString("C")
                    
                            Catch Member As Exception
                                'Handle a member exception
                                MessageBox.Show("Enter Member Number.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                With MemberTextBox
                                    .Focus()
                                    .SelectAll()
                                End With
                            End Try
                            Try
                                'Calculate the summary
                                TotalSumInteger += MemberInteger
                                MemberInteger += 1
                                'Calculate the total Rentals of the quantity videos rented
                                TotalRentalSumDecimal += QuantityInteger.ToString()
                                DiscountTextBox.Text = RentalAmountDecimal - 0.1
                                AmountDueTextBox.Text = RentalAmountDecimal - 0.1
                    
                            Catch ex As Exception
                            End Try
                    
                            Try
                                'Calculate the rentals amount
                                RentalAmountTextBox.Text = (QuantityInteger * 1.8).ToString("C")
                    
                                'Calculate the total Rentals of the quantity videos rented
                                TotalRentalSumTextBox.Text = TotalRentalSumDecimal
                    
                                TotalSumTextBox.Text = MemberInteger.ToString()
                    
                            Catch ex As Exception
                    
                            End Try
                    
                        End Sub

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #25
                      Shane:
                      I once asked you if this was your first project and you said it was your third.
                      Looking at your posts I see you started 3 threads. *ALL* of them are for the inability to calculate in a project, with only slight differences.

                      Please do not start multiple threads for the same issue. It divides efforts to help you.

                      It is becoming very clear that you need to start on a smaller scale. All three of your projects are failing for the same reason: You are trying to create programs that must go through an entire process of multiple calculations before you have worked out how to do a single calculation.

                      In addition, you need to take some time to learn your way around Visual Studio itself.

                      Microsoft Press does some good "Learn to Program in {Language}" books that are designed to teach you Visual Studio while building up your coding skills. I HIGHLY recommend dropping by the bookstore or library.

                      At the very least, you need to reduce the complexity of your calculation process. You need to learn how to take the value in A, add it to B and display it in C - before trying to do 10 times that all in one method. As I have mentioned before, having the entire process in one method means the method fails as soon as you have one back step.

                      Comment

                      Working...