Need Help with a VB Error Number of indices....
I am doing a midterm project and I am having some problems. Here is the assignment:
JM Sales employs 8 salespeople. The sales manager wants an application that allows him to enter a bonus rate. The application should use the rate, along with the 8 salespeople, in an array, to calculate each salesperson's bonus amount. The application should display each salesperson's number (1 through 8) and bonus amount. as well as the total bonus paid, in the interface.
I have the interface correct, my problem is with the section of code below where I am trying calculate the total bonus paid:
Code:
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
Dim Sales() As Integer = {2400, 1500, 1600, 2790, 1000, 6300, 1300, 2700}
Dim BonusRate As Double
Dim K As Integer = 0
Dim Report As String = "Bonus".PadLeft(7) & ControlChars.NewLine
Dim TotalSales(9) As Integer
Dim bonus() As Double
Dim TotalBonus As Double
Double.TryParse(txtRate.Text, BonusRate)
Dim ValidBonus As Boolean = BonusRate > 0
ReDim Preserve bonus(8)
If ValidBonus Then
If BonusRate > 0 Then
Do While K < 8
TotalSales(K) = Sales(K, 0) + Sales(K, 1)
bonus(K) = (TotalSales(K) * (BonusRate))
Report = Report & (K + 1).ToString().PadRight(2) & bonus(K).ToString("n2").PadLeft(5) & ControlChars.NewLine
TotalBonus = TotalBonus + bonus(K)
K = K + 1
Loop
End If
Report = Report & (K + 1).ToString().PadLeft(1) & ". " & bonus(K).ToString("n2").PadLeft(5) & ControlChars.NewLine
txtReport.Text = Report
Else
MessageBox.Show("Please enter a numerical amount")
End If
End Sub
The section below is generating the following 2 Errors:
Number of indices exceeds the number of dimensions of the indexed array
And is pointing out the 2 Red highlighted areas.
Code:
If ValidBonus Then
If BonusRate > 0 Then
Do While K < 8
TotalSales(K) = Sales(K, 0) + Sales(K, 1)
bonus(K) = (TotalSales(K) * (BonusRate))
Report = Report & (K + 1).ToString().PadRight(2) & bonus(K).ToString("n2").PadLeft(5) & ControlChars.NewLine
TotalBonus = TotalBonus + bonus(K)
K = K + 1
Loop
End If
Report = Report & (K + 1).ToString().PadLeft(1) & ". " & bonus(K).ToString("n2").PadLeft(5) & ControlChars.NewLine
txtReport.Text = Report
Else
MessageBox.Show("Please enter a numerical amount")
End If
Any help on this would be greatly appreciated! Thanks
Re: Need Help with a VB Error Number of indices....
Sales is a 1 dimensional array, I don't quite follow, what exactly are you trying to achieve by giving 2d indices.
A 1D array is a sequence of values each having an index:
Code:
Dim Sales() As Integer = {2400, 1500, 1600, 2790, 1000, 6300, 1300, 2700}
You address each element by its index, thus
Sales(0) = 2400, Sales(1) = 1500, Sales(2) = 1600, Sales(3) = 2790, etc
A 2d Array looks like a table:
Code:
' Note the empty comma between the parentheses in the array declaration:
Dim Sales(,) As Integer = {
{150, 180, 270, 360},
{501, 620, 325, 863},
{820, 128, 728, 185}
}
Here, you address each element by two indices (row index and column index):
i.e.:
Sales(1,1) = 620, Sales(0,3) = 820, Sales(3,1) = 863, etc.
In your code, Sales is declared as 1d thus you only need one index to access its members.
Re: Need Help with a VB Error Number of indices....
Yes, after searching I realized that I was specifying 2 idexes for a 1D array. Which is why I am getting the error. I understand now what the error is telling me.
I need to total all 8 bonuses, which is now where I am having trouble. I understand the math to get there(sum of all sales * by the BonusRate entered), but I am having an extremely tough time writing the code to output the sum of all bonuses in a Label named lblTotal
I am extremely tired and have been working on this for too long, I apologize if I am being unclear!
Re: Need Help with a VB Error Number of indices....
Quote:
Originally Posted by
SpeedwayNative
I need to total all 8 bonuses, which is now where I am having trouble. I understand the math to get there(sum of all sales * by the BonusRate entered), but I am having an extremely tough time writing the code to output the sum of all bonuses in a Label named lblTotal
I'm not sure you're doing it right, but Sum of all sales * bonus rate is calculates as simply as that:
Code:
Dim Sales() As Integer = {2400, 1500, 1600, 2790, 1000, 6300, 1300, 2700}
Dim Total = Sales.Sum()
Dim BonusRate As Double
Double.TryParse(txtRate.Text, BonusRate)
MsgBox(Total * BonusRate)
Re: Need Help with a VB Error Number of indices....
OK, I have figured out how to display the Total bonus paid and have it displaying properly. I actually did this before you responded, and I'm absolutely sure that I have made the code for this program much more complicated than it needs to be:blush:
The only issue I am now having is when I run this program, after entering .1 as the rate, The 8 Bonus amounts are displayed correctly, but then a 9th bonus is also displayed showing 0.00 because of course there isn’t a 9th element in the array with a sales amount. So while I understand why the amount is 0, I don’t even want it displayed, but can’t seem to figure out how to remove it. At this point I am delirious, and I just can’t see it!
Here is my new code with everything working, except a 9th Bonus being displayed as 0.00
Code:
Public Class frmMain
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
Dim Sales() As Integer = {2400, 1500, 1600, 2790, 1000, 6300, 1300, 2700}
Dim BonusRate As Double
Dim K As Integer = 0
Dim Report As String = "Bonus".PadLeft(7) & ControlChars.NewLine
Dim TotalSales(9) As Integer
Dim bonus() As Double
Dim TotalBonus As Double
Double.TryParse(txtRate.Text, BonusRate)
Dim ValidBonus As Boolean = BonusRate > 0
ReDim Preserve bonus(8)
If ValidBonus Then
If BonusRate > 0 Then
Do While K < 8
TotalSales(K) = Sales(K)
bonus(K) = (TotalSales(K) * (BonusRate))
Report = Report & (K + 1).ToString().PadRight(2) & bonus(K).ToString("n2").PadLeft(5) & ControlChars.NewLine
TotalBonus = TotalBonus + bonus(K)
K = K + 1
Loop
End If
Report = Report & (K + 1).ToString().PadLeft(1) & ". " & bonus(K).ToString("n2").PadLeft(5) & ControlChars.NewLine
txtReport.Text = Report
Else
MessageBox.Show("Please enter a numerical amount")
End If
lblTotalBonus.Text = TotalBonus.ToString("c2")
End Sub
Private Sub txtrate_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRate.Enter
txtRate.SelectAll()
End Sub
Private Sub quantityBoxKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txtRate.KeyPress
If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." Then
e.Handled = True
End If
End Sub
Private Sub Clear(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles txtRate.TextChanged
txtReport.Text = String.Empty
End Sub
End Class
Also please feel free to critique the way I have written this program, and help me better understand how to code it in a more simple manner :thumb:
Re: Need Help with a VB Error Number of indices....
As for the 'critique part' I'd say tl;dr. But
If you declare an array with 8 elements, their indices will start at 0 and end at 7. Count yourself - 0,1,2,3,4,5,6,7 - eight indices in total.
Your mistake is to add one 'extra' trailing one here:
and here:
Code:
Dim TotalSales(9) As Integer
Code:
Dim myarray(7) As Integer ' Declares an array with 8 elements in total
Dim myarray(8) As Integer ' Declares an array with 9 elements in total
Programmers start counting from zero, not one as normal people do. Yes, sometimes, even in normal life :lol:
P.S. If you want to be sure you can always perform an iteration like this:
vb Code:
For k As Integer = Sales.GetLowerBound(0) To Sales.GetUpperBound(0)
' Your cycle body here
Next
Re: Need Help with a VB Error Number of indices....
Also, here:
Code:
If ValidBonus Then
If BonusRate > 0 Then
You can get by with only one if, since you wrote earlier that:
Code:
Dim ValidBonus As Boolean = BonusRate > 0
Re: Need Help with a VB Error Number of indices....
Yea, I had tried to change it to 7 many times, and when I would enter the bonus rate, it would dump me back into the code with a section of the code highlighted and a info box with a bunch of different error info. So I kept changing it back to 8 so it would at least create the report, although adding the 9th element.
I don't know how you made me realize that the line of code it was highlighting had already been expressed in the loop and was unnecassary:
Original (wrong) section of code posted above in post #5
Code:
If ValidBonus Then
If BonusRate > 0 Then
Do While K < 8
TotalSales(K) = Sales(K)
bonus(K) = (TotalSales(K) * (BonusRate))
Report = Report & (K + 1).ToString().PadRight(2) & bonus(K).ToString("n2").PadLeft(5) & ControlChars.NewLine
TotalBonus = TotalBonus + bonus(K)
K = K + 1
Loop
End If
Report = Report & (K + 1).ToString().PadLeft(1) & ". " & bonus(K).ToString("n2").PadLeft(5) & ControlChars.NewLine
txtReport.Text = Report
Else
MessageBox.Show("Please enter a numerical amount")
End If
lblTotalBonus.Text = TotalBonus.ToString("c2")
End Sub
Corrected (and cleaned up) section:
Code:
If BonusRate > 0 Then
Do While K < 8
TotalSales(K) = Sales(K)
bonus(K) = (TotalSales(K) * (BonusRate))
Report = Report & (K + 1).ToString().PadRight(2) & bonus(K).ToString("n2").PadLeft(7) & ControlChars.NewLine
TotalBonus = TotalBonus + bonus(K)
K = K + 1
Loop
End If
txtReport.Text = Report
lblTotalBonus.Text = TotalBonus.ToString("c2")
End Sub
And finally the Entire Working Code:
Code:
Public Class frmMain
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
Dim Sales() As Integer = {2400, 1500, 1600, 2790, 1000, 6300, 1300, 2700}
Dim BonusRate As Double
Dim K As Integer = 0
Dim Report As String = "Bonus".PadLeft(8) & ControlChars.NewLine
Dim TotalSales(9) As Integer
Dim bonus() As Double
Dim TotalBonus As Double
Double.TryParse(txtRate.Text, BonusRate)
Dim ValidBonus As Boolean = BonusRate > 0
ReDim Preserve bonus(7)
If BonusRate > 0 Then
Do While K < 8
TotalSales(K) = Sales(K)
bonus(K) = (TotalSales(K) * (BonusRate))
Report = Report & (K + 1).ToString().PadRight(2) & bonus(K).ToString("n2").PadLeft(7) & ControlChars.NewLine
TotalBonus = TotalBonus + bonus(K)
K = K + 1
Loop
End If
txtReport.Text = Report
lblTotalBonus.Text = TotalBonus.ToString("c2")
End Sub
Private Sub txtrate_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRate.Enter
txtRate.SelectAll()
End Sub
Private Sub quantityBoxKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txtRate.KeyPress
If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." Then
e.Handled = True
End If
End Sub
Private Sub Clear(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles txtRate.TextChanged
txtReport.Text = String.Empty
End Sub
End Class
:thumb: Thank You So much for all you help! I really appreciate it :afrog:
Re: Need Help with a VB Error Number of indices....
I should probably mention that this line is completely useless:
Code:
Dim ValidBonus As Boolean = BonusRate > 0
Re: Need Help with a VB Error Number of indices....
LMAO, yea, that makes sense! Since I removed it from the code, it no longer needed to be Dimed.
Re: Need Help with a VB Error Number of indices....
Sorry for not noticing it, but:
vb Code:
Dim bonus() As Double
' ...
ReDim Preserve bonus(7)
can be simply:
Code:
Dim bonus(7) As Double