为什么循环在整数1上停止?

时间:2013-09-02 11:19:43

标签: vb.net loops integer

为什么循环在proov = 1上停止,但是我将它循环到10? 它应该在每轮上加+1,但程序在1开始停止。

Dim proov As Integer
        proov = 0

        Dim myClientMachineAddressList = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
        Do
            Dim myClientMachineIP = myClientMachineAddressList.AddressList(proov)
            If myClientMachineIP.AddressFamily = Sockets.AddressFamily.InterNetwork Then
                Label2.Text = myClientMachineIP.ToString()
            Else
                TextBox2.Text = "IP does not equal to IPv4"
            End If
            Thread.Sleep(1000)
            proov = proov + 1
            TextBox3.Text = proov.ToString()
        Loop Until proov = 10

3 个答案:

答案 0 :(得分:1)

如果您的地址列表少于10个,则您的代码将失败并且索引超出范围异常。

将测试条件更改为

   Do
       ....
   Loop Until proov >= myClientMachineAddressList.AddressList.Length

答案 1 :(得分:1)

尝试这样

    Dim proov As Integer = 0
    Dim myClientMachineAddressList As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
    Do While proov < myClientMachineAddressList.AddressList.Length
        Dim myClientMachineIP As System.Net.IPAddress = myClientMachineAddressList.AddressList(proov)
        'your code here
        'Debug.WriteLine(myClientMachineIP.ToString)
        proov += 1
    Loop

答案 2 :(得分:0)

Varocarbas的评论帮助很多!谢谢你先生! 引用:

  

很可能是Thread.Sleep上面的代码引发了错误。至   确认这个问题,将所有这些代码放在try ... catch块中   确认循环结束直到结束