I am having trouble getting the counts store the value from one button click to another. The formula works as far as calculating the salary and the commission and on each button click it adds one to the proper section of array but when i press the button again the count from the previous click goes away and the count shows up in the salary count that matches the most current info put in the textbox. I have tried declaring the array outside the sub under the public class. I have also tried putting the counter into a separate function. Any help would be appreciated. Here is the code I have written so far.

Public class form1
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim sales, salary, commission As Double
sales = salestxt.Text
commission = sales * 0.09
salary = commission + 200
lbSalary.Text = salary

Dim s(8)
s(0) = 0
s(1) = 0
s(2) = 0
s(3) = 0
s(4) = 0
s(5) = 0
s(6) = 0
s(7) = 0
s(8) = 0

Select Case salary
Case 200 To 299
s(0) = s(0) + 1
Case 300 To 399
s(1) = s(1) + 1
Case 400 To 499
s(2) = s(2) + 1
Case 500 To 599
s(3) = s(3) + 1
Case 600 To 699
s(4) = s(4) + 1
Case 700 To 799
s(5) = s(5) + 1
Case 800 To 899
s(6) = s(6) + 1
Case 900 To 999
s(7) = s(7) + 1
Case Is >= 1000
s(8) = s(8) + 1
End Select

lb1.Text = s(0)
lb2.Text = s(1)
lb3.Text = s(2)
lb4.Text = s(3)
lb5.Text = s(4)
lb6.Text = s(5)
lb7.Text = s(6)

lb8.Text = s(7)

lb9.Text = s(8)

End Sub 
End Class

Edited by mike_2000_17: Fixed formatting

7 Years
Discussion Span
Last Post by Teme64

Declare your array to class level scope and initialize array in form's load event, like this

Public Class Form1

  Private s() As Integer

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' Initialize array
    s(0) = 0
    s(1) = 0
    s(2) = 0
    s(3) = 0
    s(4) = 0
    s(5) = 0
    s(6) = 0
    s(7) = 0
    s(8) = 0
  End Sub
  ' The rest of the code
End Class

and remove Dim s(8), and s() = 0 statements from your button click handler. Also, take a look at this thread, where I gave you the same answer ;)


Made an error in the code above.

Use either Private s(8) As Integer or in the form's load event ReDim s(8) as the first statement.


Thank you. I thought I understood it the first time, it worked in that program but had trouble when I tried to do it with the array in this program. Thank you for re-explaining it to me. Sorry you had to do it twice. This has been something I have been struggling with for a while. Thanks to your help I wont have to struggle with it anymore.

Thank you very very much.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.