为什么我的VB程序总是提示 下标越界了呢Private Sub Timer1_Timer()MSComm1.PortOpen = TrueReDim aa(3)If k1 = 0 Then MSComm1.Output = "1" + Chr(10) + Chr(13)If k1 = 1 Then MSComm1.Output = "2" + Chr(10) + Chr(13)If k1 = 2 Then MSComm1.Out

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 16:18:46

为什么我的VB程序总是提示 下标越界了呢Private Sub Timer1_Timer()MSComm1.PortOpen = TrueReDim aa(3)If k1 = 0 Then MSComm1.Output = "1" + Chr(10) + Chr(13)If k1 = 1 Then MSComm1.Output = "2" + Chr(10) + Chr(13)If k1 = 2 Then MSComm1.Out
为什么我的VB程序总是提示 下标越界了呢
Private Sub Timer1_Timer()
MSComm1.PortOpen = True
ReDim aa(3)
If k1 = 0 Then MSComm1.Output = "1" + Chr(10) + Chr(13)
If k1 = 1 Then MSComm1.Output = "2" + Chr(10) + Chr(13)
If k1 = 2 Then MSComm1.Output = "3" + Chr(10) + Chr(13)
Do While MSComm1.InBufferCount = 0
Loop
aa = MSComm1.Input
If aa(2) > 20 Or aa(0) > 64 Then GoTo xxx
k2 = Int(aa(0) * 255 = aa(1) * 1)
Select Case k2
Case 1
p12 = Int(aa(2) * 255 = aa(3) * 1)
Case 2
p22 = Int(aa(2) * 255 = aa(3) * 1)
Case 3
p32 = Int(aa(2) * 255 = aa(3) * 1)
Case Else
End Select
k1 = k1 + 1
If k1 > 2 Then k1 = 0
xxx:
MSComm1.PortOpen = False
End Sub
在之前最初的我定义过 Dim aa() as Byte
调试指向了图片中黄色部分,可是我看不出来哪儿错了呀,

为什么我的VB程序总是提示 下标越界了呢Private Sub Timer1_Timer()MSComm1.PortOpen = TrueReDim aa(3)If k1 = 0 Then MSComm1.Output = "1" + Chr(10) + Chr(13)If k1 = 1 Then MSComm1.Output = "2" + Chr(10) + Chr(13)If k1 = 2 Then MSComm1.Out
因为你的数组里面没有第三个元素aa(2)啊,虽然你前面有一句 ReDim aa(3) ,这里看似定了aa数组最大下标为3 共计4个元素,但是 aa = MSComm1.Input 这句话会重新定aa数组的大小,也就是说aa数组的大小是根据MSComm1.Input 这个值有几个byte来定义的,实例中如果MSComm1.Input 这个值没有3个byte的长话,那么去取aa(2)的值就肯定要报下标越界了