我有与设备相关的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
有人看一看,看看为什么? 似乎我的下标超出范围并导致问题,但显然上面的代码应该可行。
答案 0 :(得分:1)
我的猜测是你从一个0(零)的玩家数量开始,并在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
现在可以更容易地看到循环的哪个部分出错了。
答案 1 :(得分:0)
我认为它是关于I或playercount值的评论是正确的。我通过将此代码放在循环结束时强制计数器在达到playercount之前停止(它停在4?)来解决它。变量我只是继续上升,因为我的if和其他语句没有正确使用。感谢所有帮助过的人。
If i = playercount then
Exit Do
elseif i < playercount then
i = i + 1
End If