| | |
Do While loop not working
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 33
Reputation:
Solved Threads: 0
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)
Private Sub btnCommission_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommission.Click Dim strComAmount As String Dim decComAmount As Decimal Dim decAverageCom As Decimal Dim decTotalOfAllCom As Decimal = 0D Dim intNumberOfEntry As Integer 'convert textbox data into decimal strComAmount = Me.txtCommission.Text decComAmount = Convert.ToDecimal(strComAmount) 'Initiate the loop While decComAmount > 0 Me.lstTally.Items.Add(decComAmount) intNumberOfEntry += 1 decTotalOfAllCom += decComAmount End While 'Calculate Average decAverageCom = decTotalOfAllCom / intNumberOfEntry 'Display Total and count Me.lblTotal.Text = decAverageCom.ToString("C") Me.lblCount.Text = CStr(intNumberOfEntry) Me.lblAverage.Text = decAverageCom.ToString("C") If decComAmount < 0 Then MessageBox.Show("Please enter more than one dollar") End If End Sub
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 1
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
'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.
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:
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.
vb Syntax (Toggle Plain Text)
Private Sub btnCommission_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommission.Click Dim strComAmount As String Dim decComAmount As Decimal Dim decAverageCom As Decimal Dim decTotalOfAllCom As Decimal = 0D Dim intNumberOfEntry As Integer 'Initiate the loop While decComAmount > 0 strComAmount = InputBox ("Enter a value","Enter value") decComAmount = Convert.ToDecimal(strComAmount) Me.lstTally.Items.Add(decComAmount) intNumberOfEntry += 1 decTotalOfAllCom += decComAmount End While 'Calculate Average decAverageCom = decTotalOfAllCom / intNumberOfEntry 'Display Total and count Me.lblTotal.Text = decAverageCom.ToString("C") Me.lblCount.Text = CStr(intNumberOfEntry) Me.lblAverage.Text = decAverageCom.ToString("C") If decComAmount < 0 Then MessageBox.Show("Please enter more than one dollar") End If 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.
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 1
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:
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.
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)
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.
![]() |
Similar Threads
- Loop not working (PHP)
- Help! Do/while loop isn't working! (C++)
- For loop problem (Java)
- problem with do...while loop (C++)
- for loop not working right (C++)
- Please help, while loop wont end (C++)
- Number Loop (C++)
- Help with gui loop. (C)
- Not working properly.. (Java)
Other Threads in the VB.NET Forum
- Previous Thread: Capture Video Recording + Media Player
- Next Thread: Crystl Report Help plzzzzzzzzzzzzzz
| Thread Tools | Search this Thread |
"crystal .net .net2005 2008 access add advanced application array assignment basic beginner box button buttons center click code combo convert cpu cuesent data database datagrid datagridview designer dissertation dissertations dissertationthesis dosconsolevb.net editvb.net employees excel exists firewall forms html image images isnumericfuntioncall listview login map math memory mobile module msaccess mssqlbackend mysql navigate net number opacity open pan pdf picturebox picturebox2 port print printpreview record regex reports" reuse right-to-left save savedialog search serial socket sorting sqldatbase sqlserver storedprocedure string temp textbox timer txttoxmlconverter upload useraccounts usercontol usercontrol vb vb.net vb.nettoolboxvisualbasic2008sidebar vba vbnet vista visual visualbasic visualbasic.net visualstudio.net web wpf wrapingcode xml





