1

this is my problem here i want to increase index of global array to return different values.

but every time i click on button it resets to old value.

here is my code....

Public Class Form1

Inherits System.Windows.Forms.Form
Dim a(5) As Decimal
Dim i As Integer
Dim j As Integer=0




Private Sub btnCalculate_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnCalculate.Click

    For i = j To j
        Dim dOrderTotal As Decimal
        Dim dDiscountPct As Decimal
        Dim dDiscountAmount As Decimal
        Dim dInvoiceTotal As Decimal

        dOrderTotal = txtOrderTotal.Text
        If dOrderTotal >= 100 Then
            dDiscountPct = 0.2
        Else
            dDiscountPct = 0
        End If
        dDiscountAmount = dOrderTotal * dDiscountPct
        dInvoiceTotal = dOrderTotal - dDiscountAmount

        lblDiscountAmount.Text = dDiscountAmount
        lblInvoiceTotal.Text = dInvoiceTotal
        txtOrderTotal.Focus()
        a(i) = dInvoiceTotal

    Next
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnExit.Click
    MessageBox.Show(a(0) & ControlChars.CrLf & a(1) & ControlChars.CrLf & a(2)& ControlChars.CrLf & a(3) & ControlChars.CrLf & a(4))



    Me.Close()
End Sub

Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub[![enter image description here][1]][1]

1 Answer 1

1

I'm mot sure that your code executes the for loop. You start from zero to zero so, probably that is why you don't see the results you expect

For i = j To j

j is always zero and is not updated in the code you have shown. The code of the subroutine to update the array's values should be:

Private Sub btnCalculate_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnCalculate.Click
    Dim dOrderTotal As Decimal
    Dim dDiscountPct As Decimal
    Dim dDiscountAmount As Decimal
    Dim dInvoiceTotal As Decimal

    dOrderTotal = txtOrderTotal.Text
    If dOrderTotal >= 100 Then
        dDiscountPct = 0.2
    Else
        dDiscountPct = 0
    End If
    dDiscountAmount = dOrderTotal * dDiscountPct
    dInvoiceTotal = dOrderTotal - dDiscountAmount

    lblDiscountAmount.Text = dDiscountAmount
    lblInvoiceTotal.Text = dInvoiceTotal
    txtOrderTotal.Focus()
    a(j) = dInvoiceTotal
    j = j + 1
    if j = 5 Then j = 0

End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

That depends on what you want to do. Do you want to goover the entire array or o update only specific items in it
1.add code to the form class so the invoice total is added to the next element in an array each time the user clicks on the calculate button. 2. add code to the exit procedure so all the elements in the array are displayed in a message box when the user clicks on the exit button.
What happens when the user clicks the button more than five times?
@JhanviDwivedi I've added the code for the subroutine to update the values in the array. since I don't know what happens after five clicks I just start again from the beginning of the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.