Can someone explain to me why this code do not try to perform the loop? It just crashes my program:(

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

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

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

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?

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:

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.