0

我有一个与设备相关的 vbscript 问题。下面的代码甚至不应该在之后执行,i = playercount但确实如此。

Do Until i = playercount

    If i = playercount then
        Exit Do
    end if

    Select Case True
        Case i >=1 And i <= 4
            'usb hubs 1-4
            'b = i + 1
             If pluggedindev(4) = False Then

                   msgbox(i)
                   msgbox(playercount)
                   Set ExtHubPort(i) = ExtHub(1).Ports(i)
                   Wscript.Sleep 2000
                   ExtHubPort(i).HotPlug GenericHIDDSFDev(i)
                   WScript.Sleep 10000
                   pluggedindev(i) = True
                   MsgBox("usb device (ports 1-4)")
             'else if playercount <= 4 then
                   'Exit Do
             End If

        Case i >=5 And i <= 8
            'usb hubs 5-8
            Wscript.sleep 2000

            For b = 1 To 4
                 Set ExtHubPort(i) = ExtHub(2).Ports(b)
                 Wscript.sleep 2000
                 ExtHubPort(i).HotPlug GenericHIDDSFDev(i)
                 WScript.Sleep 10000
                 'Log("Usb devices 1 through 4 set")
            Next 

            'usb hubs 9-12
        Case i >=9 And i <= 12
            For b = 1 To 4 
            Set ExtHubPort(i) = ExtHub(3).Ports(b)
                Wscript.sleep 2000
                ExtHubPort(i).HotPlug GenericHIDDSFDev(i)
                WScript.Sleep 10000
            Next 
           'usb hubs 13-16
        Case i >=13 And i <= 16
            For b = 1 To 4 
                Set ExtHubPort(i) = ExtHub(4).Ports(b)
                Wscript.sleep 2000
                ExtHubPort(i).HotPlug GenericHIDDSFDev(i)
                WScript.Sleep 10000
            Next 
         'usb hubs 17-20
        Case i >=17 And i <=20
            For b = 1 To 4 
                Set ExtHubPort(i) = ExtHub(5).Ports(b)
                Wscript.sleep 2000
                ExtHubPort(i).HotPlug GenericHIDDSFDev(i)
                WScript.Sleep 10000
            Next 
    End Select

    i = i + 1

Loop

有人看看,看看为什么?似乎 i 下标超出范围并导致问题,但显然上面的代码应该可以工作。

4

2 回答 2

1

我的猜测是您从 0(零)的 playercount 开始并将 i 初始化为 1(一)。条件永远不会得到满足,当i = playercount它进入原始循环的第二次迭代时会出现错误。

首先,将您的代码缩短为如下所示:

' Let ExtHubPort(1 to playercount) reference to
'     ExtHub(1 to playercount/4).Ports(1 to 4)
For i = 0 to playercount - 1
    Set ExtHubPort(i+1) = ExtHub(int(i/4)+1).Ports((i mod 4) + 1)
    Wscript.sleep 2000
    ExtHubPort(i+1).HotPlug GenericHIDDSFDev(i+1)
    WScript.Sleep 10000
Next

现在更容易看出循环的哪一部分出错了。

于 2012-11-30T16:10:36.650 回答
0

我认为关于 I 或 playercount 值的评论是正确的。我通过将此代码放在循环末尾强制计数器在达到 playercount 之前停止(它停止在 4?)来解决它。变量我只是继续上升,因为我的 if 和其他语句没有正确使用。感谢所有帮助过的人。

If i = playercount  then
Exit Do
elseif i < playercount then
i = i + 1
End If
于 2012-12-04T20:08:16.600 回答