943,510 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 3148
  • VB.NET RSS
Oct 13th, 2008
0

Do While loop not working

Expand Post »
Can someone explain to me why this code do not try to perform the loop? It just crashes my program
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub btnCommission_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommission.Click
  2. Dim strComAmount As String
  3. Dim decComAmount As Decimal
  4. Dim decAverageCom As Decimal
  5. Dim decTotalOfAllCom As Decimal = 0D
  6. Dim intNumberOfEntry As Integer
  7.  
  8. 'convert textbox data into decimal
  9. strComAmount = Me.txtCommission.Text
  10. decComAmount = Convert.ToDecimal(strComAmount)
  11.  
  12. 'Initiate the loop
  13. While decComAmount > 0
  14. Me.lstTally.Items.Add(decComAmount)
  15. intNumberOfEntry += 1
  16. decTotalOfAllCom += decComAmount
  17. End While
  18.  
  19. 'Calculate Average
  20. decAverageCom = decTotalOfAllCom / intNumberOfEntry
  21.  
  22. 'Display Total and count
  23.  
  24. Me.lblTotal.Text = decAverageCom.ToString("C")
  25. Me.lblCount.Text = CStr(intNumberOfEntry)
  26. Me.lblAverage.Text = decAverageCom.ToString("C")
  27. If decComAmount < 0 Then
  28. MessageBox.Show("Please enter more than one dollar")
  29. End If
  30.  
  31.  
  32. End Sub
Similar Threads
Reputation Points: 30
Solved Threads: 0
Light Poster
rogenie is offline Offline
33 posts
since Oct 2007
Oct 13th, 2008
0

Re: Do While loop not working

the value of ur loop in never decreasing...its always increasing..assign loop counter a value and then decrease it 1 by 1 at the end of the each loop...loop wil stop when the counter value reaches 0
domething like

decComAmount = decComAmount -1
Last edited by hisheeraz; Oct 13th, 2008 at 1:27 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hisheeraz is offline Offline
9 posts
since Oct 2008
Oct 13th, 2008
0

Re: Do While loop not working

I think hisheeraz has hit the button. the problem is that you have a test condition which remains true forever and thus sends your program into an infinite loop. this would explain the "crash" as a rule you should always ensure that your test condition has a modification statement within the loop and will have a definite end. otherwise another way to do it is to add a safety net like so:

'Initiate the loop
While decComAmount > 0 And decComAmount < 100(for example)
Me.lstTally.Items.Add(decComAmount)
intNumberOfEntry += 1
decTotalOfAllCom += decComAmount
End While
Last edited by Tyrone.Wilson; Oct 13th, 2008 at 3:54 am.
Reputation Points: 10
Solved Threads: 1
Light Poster
Tyrone.Wilson is offline Offline
25 posts
since Aug 2008
Oct 13th, 2008
0

Re: Do While loop not working

Hi guys. Thanks. I wanted the user to enter as much data as they want and put it in a listbox. then it will end when the calculate average button is clicked.

Why does it do infinite loop? If I dn't do it that way, then how can I make the user input as much number as they want?
Reputation Points: 30
Solved Threads: 0
Light Poster
rogenie is offline Offline
33 posts
since Oct 2007
Oct 13th, 2008
0

Re: Do While loop not working

If you want the user to enter as many entries as they want, then you need to allow the user to enter values within the while loop. Try something like this:

vb Syntax (Toggle Plain Text)
  1. Private Sub btnCommission_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommission.Click
  2. Dim strComAmount As String
  3. Dim decComAmount As Decimal
  4. Dim decAverageCom As Decimal
  5. Dim decTotalOfAllCom As Decimal = 0D
  6. Dim intNumberOfEntry As Integer
  7.  
  8. 'Initiate the loop
  9. While decComAmount > 0
  10. strComAmount = InputBox ("Enter a value","Enter value")
  11. decComAmount = Convert.ToDecimal(strComAmount)
  12. Me.lstTally.Items.Add(decComAmount)
  13. intNumberOfEntry += 1
  14. decTotalOfAllCom += decComAmount
  15. End While
  16.  
  17. 'Calculate Average
  18. decAverageCom = decTotalOfAllCom / intNumberOfEntry
  19.  
  20. 'Display Total and count
  21.  
  22. Me.lblTotal.Text = decAverageCom.ToString("C")
  23. Me.lblCount.Text = CStr(intNumberOfEntry)
  24. Me.lblAverage.Text = decAverageCom.ToString("C")
  25. If decComAmount < 0 Then
  26. MessageBox.Show("Please enter more than one dollar")
  27. End If
  28.  
  29.  
  30. End Sub

This code will show an input box for the user to enter data instead of using the textbox. If you want the user to use the textbox, then you need to split your code over two events, such as two command buttons: one for user input and the other to process the data.
Reputation Points: 27
Solved Threads: 29
Posting Whiz
timothybard is offline Offline
317 posts
since Mar 2007
Oct 14th, 2008
0

Re: Do While loop not working

Agreed. the problem seems to be that you are going into the loop with no chance of coming out. I see in the code you gave in the first post you only read once with:

strComAmount = Me.txtCommission.Text
decComAmount = Convert.ToDecimal(strComAmount)

after this you go into the loop. thus I don't see what you are trying to average.

The message box would work well but perhaps with an add amount button to add the number and a finish button to finish reading new numbers.

Otherwise if you really wanted to do the textbox idea then perhaps you should do something like the following (in psudo code)

1.user puts in data

2. if the user hits enter key (i.e. in the textbox_enter routine) the following is done:
-convert the entry to your decimal
-add the entry to an arraylist
-incriment the number of entries
-clear the text box
-re-select the textbox 'This is cos it will loose focus when the enter key is pressed
when the user is done then they can press a "Calculate" button. ' this way you will know when to get ready for another entry and when to calculate.

when you hit "Calculate" you will then do something like:

dim thetotal as decimal

for i=0 to myArraylist.count-1
thetotal = theTotal+myArraylist.item(i).todecimal
next

average = thetotal/(myArraylist.count-1)
again this is probably not that syntactically correct as I am writing this on the fly but the logic is about right.

also don't forget that if the user does not hit enter on their last entry you would not have read that value in. you can easily test this by checking that the textbox is in fact blank. if it isn't then do the textbox enter routine before you do your final calculate to ensure that the last entry is there.

hope this helps you do what you want. let me know if you have any questions.
Last edited by Tyrone.Wilson; Oct 14th, 2008 at 2:58 am.
Reputation Points: 10
Solved Threads: 1
Light Poster
Tyrone.Wilson is offline Offline
25 posts
since Aug 2008
Oct 14th, 2008
0

Re: Do While loop not working

hmmm.... just noticed that it is not your post at the top Rogenie, perhaps you should start your own thread. but my answer above makes some sense in any case.
Last edited by Tyrone.Wilson; Oct 14th, 2008 at 3:03 am.
Reputation Points: 10
Solved Threads: 1
Light Poster
Tyrone.Wilson is offline Offline
25 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Capture Video Recording + Media Player
Next Thread in VB.NET Forum Timeline: Crystl Report Help plzzzzzzzzzzzzzz





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC