注册 登录
编程论坛 VB6论坛

运行时总是出现错误9,“下标越界”?

wxl900306 发布于 2013-05-16 09:20, 695 次点击
程序代码:
Private Sub MSComm1_OnComm()
    Dim BytReceived() As Byte
    Dim strBuff As Variant
   
    Select Case Case 2
        MSComm1.InputLen = 0
        strBuff = MSComm1.Input
        BytReceived() = strBuff
        Dim i As Long
        For i = 0 To UBound(BytReceived)
           Debug.Print BytReceived(i)
        Next
        If i <= 2 Then
        Text2 = BytReceived(0)
        BytReceived(0) = 0
        Else
        tongyi = BytReceived(0)
        BytReceived(0) = 0
        fandui = BytReceived(2)
        BytReceived(2) = 0
        qiquan = BytReceived(4)
        BytReceived(4) = 0
        End If
    End Select
我这段代码那里有问题   为什么运行时经常出现 错误9,“下标越界”   调试时总指在qiquan = BytReceived(4)这一行
4 回复
#2
apull2013-05-16 12:27
UBound(BytReceived) 是多少?
#3
Artless2013-05-16 19:55
BytReceived下标<4
#4
wxl9003062013-05-16 20:37
回复 3楼 Artless
什么意思啊!!!
#5
tglyq2013-05-19 21:34
你开始定义数组就考虑上标最大值(比如为4),可定义为:
Dim BytReceived(4)
你也可以如果在你不知开始定义了多大上标的情况下,重新定义为你需要的最大上标,如下:
ReDim Preserve BytReceived(4)
你不妨试试。
1