-
December 12th, 2011, 05:58 PM
#1
Help with arrays, ArguementOutOfRangeExceptin was unhandled.
I get the following Error everywhere i have highlighted with *** :
ArguementOutOfRangerExceptin was unhandled. Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Public Class Form1
Dim Array(24) As Integer
Dim J As Integer = 0
Dim MyArray As New List(Of Integer)
Private Sub btnPutNumberIntoMyArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutNumberIntoArray.Click
Dim number As Integer
Integer.TryParse(txtNumbers.Text, number)
MyArray.Add(number)
txtNumbers.Clear()
txtNumbers.Focus()
End Sub
Private Sub btnDisplayNumbersInMyArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click
txtDisplayNumbers.Clear()
txtDisplayNumbers.Focus()
For J As Integer = 0 To MyArray.Count - 1
txtDisplayNumbers.Text &= MyArray(J).ToString & Environment.NewLine
Next
End Sub
Private Sub btnDisplayLargestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayLargestNumber.Click
Dim Largest As Integer
Largest = MyArray(0)
For J = 1 To 24
****If MyArray(J) > Largest Then**********************************************
Largest = MyArray(J)
End If
Next
txtDisplayNumbers.Text = CStr(Largest)
End Sub
Private Sub btnDisplaySmallestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySmallestNumber.Click
Dim Smallest As Integer
Smallest = MyArray(0)
For J = 1 To 24
*****If MyArray(J) < Smallest Then***********************************************
Smallest = MyArray(J)
End If
Next
txtDisplayNumbers.Text = CStr(Smallest)
End Sub
Private Sub btnDisplayRange_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayRange.Click
Dim Smallest As Integer
Dim Largest As Integer
Smallest = MyArray(0)
Largest = MyArray(0)
For J = 1 To 24
*****If MyArray(J) < Smallest Then***********************************************
Smallest = MyArray(J)
ElseIf MyArray(J) > Largest Then
Largest = MyArray(J)
End If
Next
txtDisplayNumbers.Text = _
CStr(Smallest) & " to " & CStr(Largest)
End Sub
Private Sub btnDisplaySumOfNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySumOfNumbers.Click
Dim Sum As Integer
For J = 0 To 24
****Sum = Sum + MyArray(J)************************************************
Next
txtDisplayNumbers.Text = CStr(Sum)
End Sub
Private Sub btnDisplayMean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayMean.Click
Dim lCounter As Long
Dim dTotal As Double
Dim dAverage As Double
dTotal = 0
For lCounter = 0 To UBound(Array)
*******dTotal = dTotal + MyArray(CInt(lCounter))**********************************
Next
dAverage = dTotal / (UBound(Array) + 1)
txtDisplayNumbers.Text = CStr(dAverage)
End Sub
End Class
but when ever I hover over Variable names in VB i get the right answer but it wont display it because of the error , HELPP!!!!
-
December 12th, 2011, 06:45 PM
#2
Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.
Arrays are 0 based. To loop through an array of 24 elements you would use 0 to 23
Notice in your code above that you are not reporting an error here
Code:
For J As Integer = 0 To MyArray.Count - 1
txtDisplayNumbers.Text &= MyArray(J).ToString & Environment.NewLine
Next
Last edited by DataMiser; December 12th, 2011 at 06:48 PM.
Always use [code][/code] tags when posting code.
-
December 12th, 2011, 06:56 PM
#3
Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.
My code has to have 25 elements, that's why its 24. Im still confused as in how to fix my code :/
-
December 12th, 2011, 07:34 PM
#4
Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.
Pick 25 for the FIRST element, and it should work.
Code:
Largest = MyArray(0)
-
December 12th, 2011, 07:51 PM
#5
Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.
Private Sub btnDisplayLargestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayLargestNumber.Click
Dim Largest As Integer
Largest = MyArray(25) <---- I CHANGED IT TO 25 BUT STILL GOT AN ERROR(i also tried 24)
For J = 1 To 24
If MyArray(J) > Largest Then
Largest = MyArray(J)
End If
Next
txtDisplayNumbers.Text = CStr(Largest)
End Sub
Error: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
-
December 12th, 2011, 07:57 PM
#6
Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.
Could you just tell me how i could insert Code2 into code1 correctly?
Code1:
Option Strict On
Public Class Form1
Dim Array(4) As Integer
Dim J As Integer = 0
Private Sub btnPutNumberIntoMyArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutNumberIntoArray.Click
Array(J) = CInt(txtNumbers.Text)
J = J + 1
txtNumbers.Clear()
txtNumbers.Focus()
End Sub
Private Sub btnDisplayNumbersInArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click
txtDisplayNumbers.Clear()
txtDisplayNumbers.Focus()
For J As Integer = 0 To Array.Count - 1
txtDisplayNumbers.Text &= Array(J).ToString & Environment.NewLine
Next
End Sub
Private Sub btnDisplayLargestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayLargestNumber.Click
Dim Largest As Integer
Largest = Array(0)
For J = 1 To 4
If Array(J) > Largest Then
Largest = Array(J)
End If
Next
txtDisplayNumbers.Text = CStr(Largest)
End Sub
Private Sub btnDisplaySmallestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySmallestNumber.Click
Dim Smallest As Integer
Smallest = Array(0)
For J = 1 To 4
If Array(J) < Smallest Then
Smallest = Array(J)
End If
Next
txtDisplayNumbers.Text = CStr(Smallest)
End Sub
Private Sub btnDisplayRange_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayRange.Click
Dim Smallest As Integer
Dim Largest As Integer
Smallest = Array(0)
Largest = Array(0)
For J = 1 To 4
If Array(J) < Smallest Then
Smallest = Array(J)
ElseIf Array(J) > Largest Then
Largest = Array(J)
End If
Next
txtDisplayNumbers.Text = _
CStr(Smallest) & " to " & CStr(Largest)
End Sub
Private Sub btnDisplaySumOfNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySumOfNumbers.Click
Dim Sum As Integer
For J = 0 To 4
Sum = Sum + Array(J)
Next
txtDisplayNumbers.Text = CStr(Sum)
End Sub
Private Sub btnDisplayMean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayMean.Click
Dim lCounter As Long
Dim dTotal As Double
Dim dAverage As Double
dTotal = 0
For lCounter = 0 To UBound(Array)
dTotal = dTotal + Array(CInt(lCounter))
Next
dAverage = dTotal / (UBound(Array) + 1)
txtDisplayNumbers.Text = CStr(dAverage)
End Sub
Private Sub btnBinarySearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBinarySearch.Click
Dim Key As Integer
Dim Low As Integer
Dim High As Integer
Dim Mid As Integer
Key = CInt(txtBinaryKey.Text)
Low = 0
High = 4
While Low <= High
Mid = (High + Low) \ 2
If Key = Array(Mid) Then
lblBinarySearchLocation.BackColor = Color.Silver
lblBinarySearch.Text = "Search Key found"
lblBinarySearchLocation.Text = "Array(" & Str(Mid) & ")"
Low = High + 1
ElseIf Key < Array(Mid) Then
High = Mid - 1
ElseIf Key > Array(Mid) Then
Low = Mid + 1
End If
End While
If Key <> Array(Mid) Then
lblBinarySearch.Text = "Search Key not found."
lblBinarySearchLocation.BackColor = Color.Red
lblBinarySearchLocation.Text = ""
End If
End Sub
End Class
*******CODE2:********
‘TOPIC: USEFUL CODE FOR CONTROLLING UNNECESSARY ZEROS IN ARRAY.
‘THERE ARE SEVERAL WAYS TO CONTROL UNNECESSARY ZEROS. THIS IS JUST ONE.
Public Class Form1
Dim mintArray() As Integer 'Declares Array without giving it a size
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) _
Handles Button1.Click
Static J As Integer
ReDim Preserve mintArray(J)
'Gives Array a size and preserves the array values
mintArray(J) = CInt(TextBox1.Text)
J=J+1
TextBox2.Clear()
'DISPLAYS THE CONTENTS OF ARRAY.
For I = 0 To J - 1
'Accumulates values in Textbox2 and displays these values.
TextBox2.Text = TextBox2.Text & CStr(mintArray(I)) & ControlChars.NewLine
Next
TextBox1.Clear()
TextBox1.Focus()
End Sub
basically how would would i add code2 into code1 one so i can get rid of the unnecessary zeros produced from not entering 25 elements, i would really appreciate it if you could hep me this.
-
December 12th, 2011, 08:44 PM
#7
Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.
Use code tags, to hard to read without formatting.
Always use [code][/code] tags when posting code.
-
December 12th, 2011, 08:49 PM
#8
Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.
You need to check the nummber of items in the array and do not loop more times than there are items. In each case where you listed an error you are looping from 1 to 24 but you are checking a list which has a variable number of items in it. In this case apparently there is 24 or less and you are trying to test at an index which is out of bounds. You also are skipping the first item in the list in case you have not noticed.
Also you are posting code and questions related to VB.Net in the VB6 area.
Always use [code][/code] tags when posting code.
-
December 14th, 2011, 11:51 AM
#9
Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.
Why don't you PRINT OUT what you *THINK* you have, to make sure you didn't overwrite one or more
-
December 14th, 2011, 03:26 PM
#10
Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.
Please use CODE tags when posting code! Really, it is not so difficult! All you have to do is type the following ( without spaces ) :
[ c o d e ] TYPE YOUR CODE HERE [ / c o d e ]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|