Loop executing twice

• Question

• Hi all,

NOT HOMEWORK! LOL!

I have a loop:

Friend Sub AnalyseThePurchases(CustomerID As Integer)
Dim NumberOfRows As Integer = 0
Dim RowCount As Byte = 1
Dim BasicString As String = ""
CustomerPurchaseTable = GetCustomerPurchases(CustomerID)
NumberOfRows = CustomerPurchaseTable.Rows.Count
MessageBox.Show(NumberOfRows.ToString)
For Each DR As DataRow In CustomerPurchaseTable.Rows
If RowCount = 12 Then
MessageBox.Show("Ending")
Exit For
End If
If DR.Item("Discount") = 0 Then
'The purchase is an eligible one - store data.
EligibleBottles = EligibleBottles + DR.Item("Quantity")
EligibleAmount = EligibleAmount + DR.Item("Paid")
CreditEarned = EligibleAmount / 15
If EligibleBottles Mod 15 = 0 Then
'Have reached a multiple of 15 bottles so release the credit available.
CreditAvailable = CreditEarned - CreditRedeemed
End If
Else
'There is a discount so check if Loyalty used (may be some residual credit
'or another discount type.
Select Case DR.Item("Reason")
Case "Loyalty points used"
'Get redeemed data.
CreditRedeemed = CreditRedeemed + DR.Item("Discount")
CreditAvailable = CreditAvailable - CreditRedeemed
Case Else
'Do nothing.
End Select

End If
DR.Item("Eligible #") = EligibleBottles
DR.Item("Eligible €") = EligibleAmount
DR.Item("Credit earned") = CreditEarned
DR.Item("Redeemed") = CreditRedeemed
DR.Item("Available") = CreditAvailable

RowCount = RowCount + 1

Next
CustomerPurchaseTable = Nothing
FormDashboard.Show()

End Sub

The problem is it runs twice and I cannot figure out why. Once the row counter is 12 it calls the ending message but thn starts again. At the end of the second loop it exits correctly.

Any ideas why?

TIA,

Dermot

Thursday, September 20, 2018 6:46 PM

All replies

• Hi

Put a breakpoint on the Exit For line and then single step - it should just jumpvout of the loop, continue single stepping to see if the calling context calls the sub again.

Regards Les, Livingston, Scotland

• Edited by Thursday, September 20, 2018 6:55 PM
Thursday, September 20, 2018 6:54 PM

• The problem is it runs twice and I cannot figure out why. Once the row counter is 12 it calls the ending message but thn starts again. At the end of the second loop it exits correctly.

If you're saying that your program appears to execute twice, make sure
you aren't seeing the result of your antivirus testing the program in
a sandbox first before allowing it to run normally. When an AV does that,
it is normal to see the program output on screen twice.

- Wayne

Friday, September 21, 2018 3:55 AM