473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
DiscountedTextBox
it only gifs me a 0.00 amount

ore any of the calculations in the summary group box
QuantitySumTextBox
DiscountSumTextBox
DiscountedAmountSumTextBox
AverageDiscountTextBox
is not calculating at all

Expand|Select|Wrap|Line Numbers
  1. Private Sub> CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
  2. 'Calculate the price and discount
  3. Dim QuantityInteger As Integer
  4. Dim PriceDecimal, ExtendedPriceDecimal, DiscountDecimal, DiscountedPriceDecimal, AverageDiscountDecimal, _
  5. DISCOUNT_RATE_Decimal, ExtendedPriceDecilmal, QuantitySumInteger, DiscountSumDecimal, SaleCountInteger, _
  6. DiscountedSumDecimal, AverageDescountDecimal As Decimal
  7. Try
  8. 'Convert quantity to numeric variable
  9. QuantityInteger = Integer.Parse(QuantityTextBox.Text)
  10. Catch ex As Exception
  11. End Try
  12. Try
  13. 'Convertprice if quantity was successful.
  14. PriceDecimal = Decimal.Parse(PriceTextBox.Text)
  15. 'Calculate values for sale.
  16. ExtendedPriceDecimal = QuantityInteger * PriceDecimal
  17. DiscountDecimal = Decimal.Round( _
  18. (ExtendedPriceDecimal * DISCOUNT_RATE_Decimal), 1)
  19. DiscountedPriceDecimal = ExtendedPriceDecilmal - DiscountDecimal
  20.  
  21. 'Calculate summary values.
  22. QuantitySumInteger += QuantityInteger
  23. DiscountSumDecimal += DiscountDecimal
  24. SaleCountInteger += 1
  25. AverageDiscountDecimal = DiscountSumDecimal / SaleCountInteger
  26.  
  27. 'Format and display answer for the sale
  28. ExtendedPriceTextBox.Text = ExtendedPriceDecimal.ToString("C")
  29. DiscountTextBox.Text = DiscountDecimal.ToString("N")
  30. DiscountedTextBox.Text = DiscountedPriceDecimal.ToString("C")
  31. 'Format and display summary values.
  32. QuantitySumTextBox.Text = QuantitySumInteger.ToString()
  33. DiscountTextBox.Text = DiscountedSumDecimal.ToString("C")
  34. AverageDiscountTextBox.Text = AverageDescountDecimal.ToString("C")
  35.  
  36. Catch PriceException As FormatException
  37. 'Handle a price exception
  38. MessageBox.Show("Price must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  39. With PriceTextBox
  40. .Focus()
  41. .SelectAll()
  42. End With
  43. End Try
  44. Try
  45. Catch QuantityException As FormatException
  46. 'Handle a quantity exception.
  47. MessageBox.Show("Quantity must be numeric. ", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  48. With QuantityTextBox
  49. .Focus()
  50. .SelectAll()
  51. End With
  52. End Try
  53. End Sub

Thanks
Apr 14 '10 #1
24 2910
tlhintoq
3,525 Expert 2GB
TIP: When you first created your question you were asked to wrap your code with [code] tags.

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.
Apr 14 '10 #2
tlhintoq
3,525 Expert 2GB
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?
Apr 14 '10 #3
MrMancunian
569 Expert 512MB
Did you do what tlhintoq told you to do? I think you just reposted your code without trying...

Steven
Apr 15 '10 #4
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
Apr 15 '10 #5
MrMancunian
569 Expert 512MB
Any error message?
Apr 15 '10 #6
Hi
Once I debug it gives me a message
( A first chance exception of type 'System.FormatException' 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
Apr 15 '10 #7
tlhintoq
3,525 Expert 2GB
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.FormatException' 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.
Apr 15 '10 #8
Hi

Yes this is my 3rd project
I have put in a breakpoint and went from line to line with F10
from this line
Expand|Select|Wrap|Line Numbers
  1.  Const DISCOUNT_RATE_Decimal As Decimal = 0.15D 
and it stops by this line
Expand|Select|Wrap|Line Numbers
  1.  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.FormatException' occurred in mscorlib.dll [/message]

wat can be wrong and how do i fix it
can you pleas help
Apr 16 '10 #9
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. 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
Apr 17 '10 #10
Hi

It is still not working

I cant change the
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. Const DISCOUNT_RATE_Decimal As Decimal = 0.15D
as a Decimal
Apr 17 '10 #11
tlhintoq
3,525 Expert 2GB
Do you have to supply the 'D' at all?
Can't you just say
Const DISCOUNT_RATE_Decimal As Decimal = 0.15

I can do that in C#
Apr 17 '10 #12
Hi

Yes , I can remove the 'D' but it doesn't make to program to calculate
it is still not working
Apr 17 '10 #13
mshmyob
904 Expert 512MB
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,
Apr 17 '10 #14
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


Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3. 'Declare module-level variables and constants
  4.     Private QuantitySumInteger, SaleCountinteger As Integer
  5.     Private DiscountSumDecimal, DiscountedPriceSumDecimal As Decimal
  6.     Const DISCOUNT_RATE_Decimal As Decimal = 0.15D
  7.  
  8.     Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
  9.         'Calculate the price and discount
  10.  
  11.         Dim QuantityInteger As Integer
  12.         Dim PriceDecimal, ExtendedPriceDecimal, DiscountDecimal, DiscountedPriceDecimal, AverageDiscountDecimal, _
  13.          DISCOUNT_RATE_Decimal, ExtendedPriceDecilmal, SaleCountInteger, _
  14.            DiscountedSumDecimal, AverageDescountDecimal As Decimal
  15.         Try
  16.             'Convert quantity to numeric variable
  17.             QuantityInteger = Integer.Parse(QuantityTextBox.Text)
  18.         Catch ex As Exception
  19.         End Try
  20.  
  21.  
  22. Try
  23.             'Convertprice if quantity was successful.
  24.  
  25.             PriceDecimal = Decimal.Parse(PriceTextBox.Text)
  26.             'Calculate values for sale.
  27.             ExtendedPriceDecimal = QuantityInteger * PriceDecimal
  28.             DiscountDecimal = Decimal.Round( _
  29.             (ExtendedPriceDecimal * DISCOUNT_RATE_Decimal), 2)
  30.             DiscountedPriceDecimal = ExtendedPriceDecilmal - DiscountDecimal
  31.  
  32.  
  33.             'Calculate summary values.
  34.             QuantitySumInteger += QuantityInteger
  35.             DiscountSumDecimal += DiscountDecimal
  36.             SaleCountInteger += 1
  37.             AverageDiscountDecimal = DiscountSumDecimal / SaleCountInteger
  38.  
  39.  
  40.             'Format and display answer for the sale
  41.             ExtendedPriceTextBox.Text = ExtendedPriceDecimal.ToString("C")
  42.             DiscountTextBox.Text = DiscountDecimal.ToString("N")
  43.             DiscountedPriceTextBox.Text = DiscountedPriceDecimal.ToString("C")
  44.  
  45.             'Format and display summary values.
  46.  
  47.             QuantitySumTextBox.Text = QuantitySumInteger.ToString()
  48.             DiscountTextBox.Text = DiscountedSumDecimal.ToString("C")
  49.             AverageDiscountTextBox.Text = AverageDescountDecimal.ToString("C")
  50.  
  51.  
  52.         Catch PriceException As FormatException
  53.             'Handle a price exception
  54.             MessageBox.Show("Price must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  55.             With PriceTextBox
  56.                 .Focus()
  57.                 .SelectAll()
  58.             End With
  59.         End Try
  60.         Try
  61.         Catch QuantityException As FormatException
  62.             'Handle a quantity  exception.
  63.             MessageBox.Show("Quantity must be numeric. ", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  64.             With QuantityTextBox
  65.                 .Focus()
  66.                 .SelectAll()
  67.             End With
  68.         End Try
  69.     End Sub
  70.  
Apr 17 '10 #15
tlhintoq
3,525 Expert 2GB
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.FormatException' occurred in mscorlib.dll [/message]
You know you have a format exception. So stop trying to integrate formating in your calls.
Expand|Select|Wrap|Line Numbers
  1. 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|Wrap|Line Numbers
Const DISCOUNT_RATE_Decimal 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.
Expand|Select|Wrap|Line Numbers
  1. Dim QuantityInteger As Integer
Line 11 from your sample above.
Now you can step through the method causing trouble until you find the bug.
Apr 18 '10 #16
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 "QuantityTextBox"+ "PriceTextBox" ="ExtendedPriceTextBox"
Apr 19 '10 #17
mshmyob
904 Expert 512MB
I am a little confused again.

I now see where you are declaring DISCOUNT_RATE_Decimal (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,
Apr 19 '10 #18
Hi

ok I left DISCOUNT_RATE_Decimal as a Const
and deleted the one that was extra in dimensioning(line 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
Apr 19 '10 #19
Hi

and it is giving me this message "A first chance exception of type 'System.DivideByZeroException' occurred in mscorlib.dll" and "A first chance exception of type 'System.FormatException' occurred in mscorlib.dll"

where can I look for the code so then I can fix it
Apr 19 '10 #20
mshmyob
904 Expert 512MB
@shanehenery
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 DiscountedSumDecimal 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,
Apr 19 '10 #21
tlhintoq
3,525 Expert 2GB
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.
Apr 19 '10 #22
have you found the solution?
Apr 19 '10 #23
Hi

I have a problem in my coding I will like to deduct 15% from my RentalAmountTextBox to be displayed in my
AmountDueTextBox

Expand|Select|Wrap|Line Numbers
  1. AmountDueTextBox.Text = RentalAmountDecimal - 0.1
then in my AmountDueTextBox it only displays -0.1
it is not doing the calculation
an you pleas help

Expand|Select|Wrap|Line Numbers
  1. Public Class VideoBonanza
  2.     'Declare module-level variables and constants.
  3.  
  4.     Private TotalSumInteger, MemberInteger, SaleCountInteger As Integer
  5.     Private TotalRentalSumDecimal As Decimal
  6.     Const Discount_Rate_Decimal As Decimal = 0.1D
  7.  
  8.     Private Sub VideoBonanza_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9.  
  10.     End Sub
  11.  
  12.     Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
  13.         'Calculate the rental amounts 
  14.  
  15.         Dim QuantityInteger As Integer
  16.         Dim RentalAmountDecimal, DiscountDecimal, AmountDueDecimal As Decimal
  17.  
  18.         Try
  19.             'Convert Quantity to numeric variable.
  20.             QuantityInteger = Integer.Parse(QuantityTextBox.Text)
  21.             AmountDueTextBox.Text = (RentalAmountDecimal - DiscountDecimal).ToString("C")
  22.             DiscountDecimal = Decimal.Round(RentalAmountDecimal * Discount_Rate_Decimal, 2)
  23.             AmountDueDecimal = RentalAmountDecimal - DiscountDecimal
  24.             RentalAmountTextBox.Text = RentalAmountDecimal.ToString("C")
  25.             AmountDueTextBox.Text = AmountDueDecimal.ToString("C")
  26.  
  27.         Catch Member As Exception
  28.             'Handle a member exception
  29.             MessageBox.Show("Enter Member Number.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  30.             With MemberTextBox
  31.                 .Focus()
  32.                 .SelectAll()
  33.             End With
  34.         End Try
  35.         Try
  36.             'Calculate the summary
  37.             TotalSumInteger += MemberInteger
  38.             MemberInteger += 1
  39.             'Calculate the total Rentals of the quantity videos rented
  40.             TotalRentalSumDecimal += QuantityInteger.ToString()
  41.             DiscountTextBox.Text = RentalAmountDecimal - 0.1
  42.             AmountDueTextBox.Text = RentalAmountDecimal - 0.1
  43.  
  44.         Catch ex As Exception
  45.         End Try
  46.  
  47.         Try
  48.             'Calculate the rentals amount
  49.             RentalAmountTextBox.Text = (QuantityInteger * 1.8).ToString("C")
  50.  
  51.             'Calculate the total Rentals of the quantity videos rented
  52.             TotalRentalSumTextBox.Text = TotalRentalSumDecimal
  53.  
  54.             TotalSumTextBox.Text = MemberInteger.ToString()
  55.  
  56.         Catch ex As Exception
  57.  
  58.         End Try
  59.  
  60.     End Sub
  61.  
Apr 20 '10 #24
tlhintoq
3,525 Expert 2GB
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.
Apr 20 '10 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Aspersion | last post by:
I'm building an ASP page that has a lot of text and graphics. There is a calculation facility on the page. The user enters several numbers in a form and presses a button to see the calculated...
10
by: Michael G | last post by:
double A = floor((2447297.0 - 122.1)/365.25); i get differing results. It varies between 6699 and 6700. The correct answer is 6699. The 6700 result really throws the entire group of calculations...
0
by: anaxamandr | last post by:
Hi. I have a long loop in ASP that performs a rather lengthy calculation. I would love for my users to be able to stop that calculation, if they so choose, mid way through the process. I attempted...
0
by: gavo | last post by:
Hi. using A2K; i have a form containing a continous subform. The question is, how can i call a calculation for one of the fields in the continous subform from the main form. At the moment i...
4
by: Michiel Alsters | last post by:
Hello everybody, I hope anybody can help me. I'll try to give a brief overview of my problem. I have running a program that performs a heavy calculation. To give the user feedback what the...
7
by: th-ko | last post by:
Hi, I would like to know whether a certain coding pattern exist to handle the following problem. I am using a const methode to get and value a from the class. In case the value was not calculated...
10
by: 60325 | last post by:
This is the page where I collect the data in drop-down boxes with values of 1-10 and send it to a submitted page to do calculations. Example: Employee1 TeamScore(1-10) Employee2 ...
5
by: The alMIGHTY N | last post by:
Hi all, Let's say I have a simple math formula: sum (x * y / 1000) / (sum z / 1000) I have to do this across 50 items, each with an x, y and z value, when the page first loads AND when a...
3
by: mattmao | last post by:
Okay, I was asked by a friend about the result of this limit: http://bbs.newwise.com/attdata/forumid_14/20070922_fe7f77c81050413a20fbDWYOGm7zeRj3.jpg Not n->zero but n-> + infinite I really...
1
by: csolomon | last post by:
I have an unbound text box which I use to get a calculation, called DM_SampleWt. This text box's control source is: =GetSize(,Nz(,0),.!,.!.(2)) I have written a function that will get the required...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.