|
-
Nov 14th, 2015, 02:50 PM
#1
Thread Starter
Addicted Member
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
-
Nov 14th, 2015, 03:29 PM
#2
Re: IndexOutOfRangeException in pinging application
Try this:
Code:
For count As Integer = 0 To amountOfTests - 1
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 14th, 2015, 04:05 PM
#3
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.
-
Nov 14th, 2015, 04:21 PM
#4
Re: IndexOutOfRangeException in pinging application
 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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 14th, 2015, 09:05 PM
#5
Thread Starter
Addicted Member
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?
 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.
-
Nov 14th, 2015, 09:20 PM
#6
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 14th, 2015, 10:58 PM
#7
Thread Starter
Addicted Member
Re: IndexOutOfRangeException in pinging application
I removed the count += 1. Why do I have to? Does it automagically increment by 1?
 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
-
Nov 14th, 2015, 11:26 PM
#8
Thread Starter
Addicted Member
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.
Last edited by Reapism; Nov 14th, 2015 at 11:38 PM.
-
Nov 14th, 2015, 11:27 PM
#9
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 14th, 2015, 11:29 PM
#10
Thread Starter
Addicted Member
Re: IndexOutOfRangeException in pinging application
OH, thanks for explaining that bit.
 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.
Tags for this Thread
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
|