Results 1 to 10 of 10

Thread: IndexOutOfRangeException in pinging application

  1. #1

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Exclamation 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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: IndexOutOfRangeException in pinging application

    Try this:

    Code:
    For count As Integer = 0 To amountOfTests - 1

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: IndexOutOfRangeException in pinging application

    Typically, you do not modify the indexing variable (count) within the For-Next block.
    Remove
    Code:
    count += 1
    from the For-Next blocks or change to one of the Do loop constructs.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: IndexOutOfRangeException in pinging application

    Quote Originally Posted by TnTinMN View Post
    Typically, you do not modify the indexing variable (count) within the For-Next block.
    Remove
    Code:
    count += 1
    from the For-Next blocks or change to one of the Do loop constructs.
    Didn't spot that...

  5. #5

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    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 View Post
    Typically, you do not modify the indexing variable (count) within the For-Next block.
    Remove
    Code:
    count += 1
    from the For-Next blocks or change to one of the Do loop constructs.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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:

    Code:
    count += 1
    from your PingTest code

  7. #7

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    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. View Post
    change:

    Code:
    For count As Integer = 0 To amountOfTests - 1
        average = average + testIterations(count)
    Next
    in your AverageOfTests function

    and remove:

    Code:
    count += 1
    from your PingTest code

  8. #8

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    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.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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.

  10. #10

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: IndexOutOfRangeException in pinging application

    OH, thanks for explaining that bit.

    Quote Originally Posted by .paul. View Post
    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
  •  



Click Here to Expand Forum to Full Width