IndexOutOfRangeException in pinging application
I am having an exception when running the function PingTest() in terms of the array I'm using. I'm not entirely sure how this is happening. Here is all the code.
Code:
Dim pingTo As String = "www.google.com"
Dim amountOfTests As Integer = 10
Dim testIterations(amountOfTests - 1) As Integer
Public Function AverageOfTests() As Boolean
Dim average As Double
For count As Integer = 0 To amountOfTests
average = average + testIterations(count)
count += 1
Next
txtResults.Text += average
Return True
End Function
Public Function PingTest() As Boolean
For count As Integer = testIterations(count) To amountOfTests
count += 1
Dim ping As New System.Net.NetworkInformation.Ping
Dim ms = ping.Send(pingTo).RoundtripTime()
txtResults.Text += System.Environment.NewLine
txtResults.Text += "pinged (" & pingTo & ") at " & ms & "."
testIterations(count) = CInt(ms)
Next
AverageOfTests()
Return True
End Function
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
txtResults.Clear()
PingTest()
End Sub
Re: IndexOutOfRangeException in pinging application
Try this:
Code:
For count As Integer = 0 To amountOfTests - 1
Re: IndexOutOfRangeException in pinging application
Typically, you do not modify the indexing variable (count) within the For-Next block.
Remove from the For-Next blocks or change to one of the Do loop constructs.
Re: IndexOutOfRangeException in pinging application
Quote:
Originally Posted by
TnTinMN
Typically, you do not modify the indexing variable (count) within the For-Next block.
Remove
from the For-Next blocks or change to one of the Do loop constructs.
Didn't spot that...
Re: IndexOutOfRangeException in pinging application
I get an error under the AverageOfTests() function
Error:
System.IndexOutOfRangeException
The error occurs on the line
average = average + testIterations(count)
New code:
Code:
Dim pingTo As String = "www.google.com"
Dim amountOfTests As Integer = 10
Dim testIterations(amountOfTests - 1) As Integer
Public Function AverageOfTests() As Boolean
Dim average As Double
For count As Integer = 0 To amountOfTests
average = average + testIterations(count)
count += 1
Next
txtResults.Text += average
Return True
End Function
Public Function PingTest() As Boolean
For count As Integer = 0 To amountOfTests - 1
count += 1
Dim ping As New System.Net.NetworkInformation.Ping
Dim ms = ping.Send(pingTo).RoundtripTime()
txtResults.Text += System.Environment.NewLine
txtResults.Text += "pinged (" & pingTo & ") at " & ms & "."
testIterations(count) = CInt(ms)
Next
AverageOfTests()
Return True
End Function
Also for this, I need a value to increment to stop it at some point. What would be the correct way to do that?
Quote:
Originally Posted by
TnTinMN
Typically, you do not modify the indexing variable (count) within the For-Next block.
Remove
from the For-Next blocks or change to one of the Do loop constructs.
Re: IndexOutOfRangeException in pinging application
change:
Code:
For count As Integer = 0 To amountOfTests - 1
average = average + testIterations(count)
Next
in your AverageOfTests function
and remove:
from your PingTest code
Re: IndexOutOfRangeException in pinging application
I removed the count += 1. Why do I have to? Does it automagically increment by 1?
Quote:
Originally Posted by
.paul.
change:
Code:
For count As Integer = 0 To amountOfTests - 1
average = average + testIterations(count)
Next
in your AverageOfTests function
and remove:
from your PingTest code
Re: IndexOutOfRangeException in pinging application
Also
Code:
Dim response As String = InputBox("Enter an IP address or website.")
If Len(response) > 0 Then
If response.Contains("http://") Then
MsgBox("Please enter a valid website address.")
pingTo = "www.google.com"
Else
response.Replace("http://", Nothing)
response.Replace("https://", Nothing)
End If
Else
End If
Also, the contains ("https://") executes even though the string doesn't contain it.
EDIT I FIXED MY LOGICAL ERROR.
Re: IndexOutOfRangeException in pinging application
Code:
for x as integer = 1 to 5
msgbox(x.ToString)
next
5 msgboxes will pop up one after another... 1,2,3,4,5
The for next block increments itself.
Re: IndexOutOfRangeException in pinging application
OH, thanks for explaining that bit.
Quote:
Originally Posted by
.paul.
Code:
for x as integer = 1 to 5
msgbox(x.ToString)
next
5 msgboxes will pop up one after another... 1,2,3,4,5
The for next block increments itself.