# Need help with arrays please!

Hi! I am working on the following problem and am having no luck with my arrays. I don't understand my book's explanation, so any help you can give me would be greatly appreciated. (And if anyone could recommend a good beginner's site, thank you!) The problem is as follows, the program should use a one dimensional array. It takes the input of a sales person and calculates the commission earned and out puts the total pay. (I have that part working fine!) Then, using an array of counters, determine how many people earn different salary ranges (\$200 - 299, \$300 - 399, ..., \$1000 < or greater)and output that. I have no idea what I am doing wrong, maybe it is a problem with my select case statement???? I have pasted my code below. Thank you for your help!
Linda

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateEarnings.Click

Dim UserEntry As Integer 'user's input of employee's wage

'converts text to number values
UserEntry = Convert.ToDouble(Me.WageInput.Text)

'this rounds the wages input by user
UserEntry = Math.Ceiling(UserEntry)

'call to calculate user's commission and total pay
CalculatePay(UserEntry)

End Sub

Function CalculatePay(ByVal entry As Integer) As Integer

Dim TotalPay As Integer 'total pay including commission

'calculates employee's total pay
TotalPay = ((entry * 0.09) + 200 + entry)

'outputs total pay to text box
Me.TotalEarningsOutput.Text = String.Format("{0}", TotalPay)

End Function

Private Sub DisplayArrayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayArrayButton.Click

Dim CalcPay As Integer 'total pay calculated
'assigns the total pay in output box to calcpay variable
CalcPay = TotalEarningsOutput.Text

'converts calcpay to numerical value
CalcPay = Convert.ToDouble(Me.TotalEarningsOutput.Text)

'calls function sort_pay
Sort_Pay(CalcPay)

End Sub

Function Sort_Pay(ByVal TotPay As Integer) As Integer
Dim i As Integer
Dim frequencyArray() As Integer 'declares the array
frequencyArray = New Integer(8) {} 'allocates memory for the array
ReDim frequencyArray(8)
ReDim Preserve frequencyArray(8)

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

For i = 0 To 8
Me.ArrayOutput().Text = String.Format("{0}", frequencyArray(i))
Next

End Function
End Class

• : Hi! I am working on the following problem and am having no luck with my arrays. I don't understand my book's explanation, so any help you can give me would be greatly appreciated. (And if anyone could recommend a good beginner's site, thank you!) The problem is as follows, the program should use a one dimensional array. It takes the input of a sales person and calculates the commission earned and out puts the total pay. (I have that part working fine!) Then, using an array of counters, determine how many people earn different salary ranges (\$200 - 299, \$300 - 399, ..., \$1000 < or greater)and output that. I have no idea what I am doing wrong, maybe it is a problem with my select case statement???? I have pasted my code below. Thank you for your help!
: Linda
:
: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateEarnings.Click
:
: Dim UserEntry As Integer 'user's input of employee's wage
:
: 'converts text to number values
: UserEntry = Convert.ToDouble(Me.WageInput.Text)
:
: 'this rounds the wages input by user
: UserEntry = Math.Ceiling(UserEntry)
:
: 'call to calculate user's commission and total pay
: CalculatePay(UserEntry)
:
: End Sub
:
: Function CalculatePay(ByVal entry As Integer) As Integer
:
: Dim TotalPay As Integer 'total pay including commission
:
: 'calculates employee's total pay
: TotalPay = ((entry * 0.09) + 200 + entry)
:
: 'outputs total pay to text box
: Me.TotalEarningsOutput.Text = String.Format("{0}", TotalPay)
:
: End Function
:
: Private Sub DisplayArrayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayArrayButton.Click
:
: Dim CalcPay As Integer 'total pay calculated
: 'assigns the total pay in output box to calcpay variable
: CalcPay = TotalEarningsOutput.Text
:
: 'converts calcpay to numerical value
: CalcPay = Convert.ToDouble(Me.TotalEarningsOutput.Text)
:
: 'calls function sort_pay
: Sort_Pay(CalcPay)
:
: End Sub
:
: Function Sort_Pay(ByVal TotPay As Integer) As Integer
: Dim i As Integer
: Dim frequencyArray() As Integer 'declares the array
: frequencyArray = New Integer(8) {} 'allocates memory for the array
: ReDim frequencyArray(8)
: ReDim Preserve frequencyArray(8)
:
: Select Case TotPay
: Case Is > 1000
: frequencyArray(8) += 1
: Case 900 To 999
: frequencyArray(7) += 1
: Case 800 To 899
: frequencyArray(6) += 1
: Case 700 To 799
: frequencyArray(5) += 1
: Case 600 To 699
: frequencyArray(4) += 1
: Case 500 To 599
: frequencyArray(3) += 1
: Case 400 To 499
: frequencyArray(2) += 1
: Case 300 To 399
: frequencyArray(1) += 1
: Case 200 To 299
: frequencyArray(0) += 1
: End Select
:
: For i = 0 To 8
: Me.ArrayOutput().Text = String.Format("{0}", frequencyArray(i))
: Next
:
: End Function
: End Class
:
:
:
:
Linda I got lost with your code. I wrote someting that might help. Look this over and see if it helps. Some observations. Your Select is fine. There is no need to ReDim the array. THat is used when you don't know what the size of the array will be. Additionally, if you are going to ReDim, you don't do it twice. The Preserve is to keep the contents of the array when you change its size. When you ReDim'ed it, then ReDim'ed it again the Preserve wouldn't help, you would have already lost the contents on the first ReDim.

Public Class Form1
Inherits System.Windows.Forms.Form
Dim arrSalaries(8) As Integer 'Array to hold earnings categories

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnCalculatePay As System.Windows.Forms.Button
Friend WithEvents txtPayInput As System.Windows.Forms.TextBox
Friend WithEvents txtTotalEarnings As System.Windows.Forms.TextBox
Friend WithEvents txtDisplayCalculatedPay As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents btnDisplayTotalPay As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.btnCalculatePay = New System.Windows.Forms.Button()
Me.txtPayInput = New System.Windows.Forms.TextBox()
Me.txtTotalEarnings = New System.Windows.Forms.TextBox()
Me.txtDisplayCalculatedPay = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.btnDisplayTotalPay = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'btnCalculatePay
'
Me.btnCalculatePay.Location = New System.Drawing.Point(228, 21)
Me.btnCalculatePay.Name = "btnCalculatePay"
Me.btnCalculatePay.Size = New System.Drawing.Size(100, 23)
Me.btnCalculatePay.TabIndex = 0
Me.btnCalculatePay.Text = "Calculate Pay"
'
'txtPayInput
'
Me.txtPayInput.Location = New System.Drawing.Point(117, 3)
Me.txtPayInput.Name = "txtPayInput"
Me.txtPayInput.TabIndex = 1
Me.txtPayInput.Text = ""
'
'txtTotalEarnings
'
Me.txtTotalEarnings.Location = New System.Drawing.Point(17, 151)
Me.txtTotalEarnings.Multiline = True
Me.txtTotalEarnings.Name = "txtTotalEarnings"
Me.txtTotalEarnings.Size = New System.Drawing.Size(193, 143)
Me.txtTotalEarnings.TabIndex = 2
Me.txtTotalEarnings.Text = ""
'
'txtDisplayCalculatedPay
'
Me.txtDisplayCalculatedPay.Location = New System.Drawing.Point(117, 43)
Me.txtDisplayCalculatedPay.Name = "txtDisplayCalculatedPay"
Me.txtDisplayCalculatedPay.TabIndex = 3
Me.txtDisplayCalculatedPay.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(9, 7)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(87, 13)
Me.Label1.TabIndex = 4
Me.Label1.Text = "Input Pay:"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(9, 47)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(87, 13)
Me.Label2.TabIndex = 5
Me.Label2.Text = "Calculated Pay:"
'
'btnDisplayTotalPay
'
Me.btnDisplayTotalPay.Location = New System.Drawing.Point(34, 101)
Me.btnDisplayTotalPay.Name = "btnDisplayTotalPay"
Me.btnDisplayTotalPay.Size = New System.Drawing.Size(158, 23)
Me.btnDisplayTotalPay.TabIndex = 6
Me.btnDisplayTotalPay.Text = "Display Total Pay"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(346, 314)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnDisplayTotalPay, Me.Label2, Me.Label1, Me.txtDisplayCalculatedPay, Me.txtTotalEarnings, Me.txtPayInput, Me.btnCalculatePay})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub btnCalculatePay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculatePay.Click
Dim dblSalaryInput As Double 'Double because of calcs later

'Convert string input in textbox to double for calculations later
dblSalaryInput = Convert.ToDouble(txtPayInput.Text)

'Round salary input (actually should test input to make sure the input
'values are actually numbers and have no more than .00 rather than rounding).
'Any one could enter a salary of 345.475960385 and it is acceptable.
dblSalaryInput = Math.Round(dblSalaryInput)

'Calculate actual pay (including commision)(I assume these calcs are correct)
dblSalaryInput = ((dblSalaryInput * 0.09) + 200 + dblSalaryInput)

'Display what we calculated
txtDisplayCalculatedPay.Text = dblSalaryInput

'Add salary to earnings array in proper category
End Sub

Private Sub AddSalariesToArray(ByVal dblSalary As Double)
'Subroutine takes calculated salaries and adds them to the array of
'salaries in the proper category
Select Case dblSalary
Case Is >= 1000 : arrSalaries(8) += 1 'Salary = or > 1000 add 1 to array element
Case 900 To 999 : arrSalaries(7) += 1 'Salary in this range add 1 to array element
Case 800 To 899 : arrSalaries(6) += 1 'etc
Case 700 To 799 : arrSalaries(5) += 1
Case 600 To 699 : arrSalaries(4) += 1
Case 500 To 599 : arrSalaries(3) += 1
Case 400 To 499 : arrSalaries(2) += 1
Case 300 To 399 : arrSalaries(1) += 1
Case 200 To 299 : arrSalaries(0) += 1
End Select
End Sub

Private Sub btnDisplayTotalPay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplayTotalPay.Click
'Subroutine to display the calculated salaries from the Salaries array by category
txtTotalEarnings.Text = "Salaries:" & vbCrLf & vbCrLf & _
" > \$1000 " & arrSalaries(8) & vbCrLf & _
"\$900 to \$999 " & arrSalaries(7) & vbCrLf & _
"\$800 to \$899 " & arrSalaries(6) & vbCrLf & _
"\$700 to \$799 " & arrSalaries(5) & vbCrLf & _
"\$600 to \$699 " & arrSalaries(4) & vbCrLf & _
"\$500 to \$599 " & arrSalaries(3) & vbCrLf & _
"\$400 to \$499 " & arrSalaries(2) & vbCrLf & _
"\$300 to \$399 " & arrSalaries(1) & vbCrLf & _
"\$200 to \$299 " & arrSalaries(0)
End Sub
End Class
• Dear Linda ,,

I think that you start Programming with VB.NET or you dont programmin as a professional . So , I have to tell you welcome to programmin zone.
Its about 7 years that I work as a programmer for companies , and I love computer programmin and Have to tell you Wow , Good choice ( I mean VB.NET ) . So If you need help about VB.NET or VB plz send me your source code and I will reply it as soon as possible . But plz attention to names that you choose 4 your objects and variables . That will help me to read ur codes easily .. Let me tell you a secret that makes you a real programmer . PLZ learn API functions .. you knwo . you can do everything with API functions in both VB and .NET all in all check www.vbcode.com out

THANX
BEST REGARDS
BOBBY PROGRAMMER
MAIL : BOBBY_PROGRAMMER@HOTMAIL.COM

• Thanks so much for your offer of help. You are right, I don't program professionally, this is my first class, and I am not learning easily! I am sure I will be taking you up on your offer of help very soon!

Thanks again,

Linda
• Hello again! Thanks so much for your help. Sorry for the messed up code. I had been trying to change bits and peices and ended up posting a bigger mess than I started with. It's all working great now, thanks to your help!

Thanks again,

Linda