为什么老说我下表越界

来源:百度知道 编辑:UC知道 时间:2024/05/11 01:16:23
Private Sub cmdPress_Click()
f = txtDep.Text
If Val(txtDep.Text) <= 0 Then
e = MsgBox("Invalid amount", 51, "message")
txtDep.Text = ""
Else
b = b + f
txtBal.Text = "$" + Format(b, "0.00")
txtDep.Text = ""
X = MsgBox("Balance is updated", 64, "message")
amount(size) = Val(txtDep.Text)
If size = 1 Then
Balance(size) = amount(size)
Else
Balance(size) = Balance(size - 1) + amount(size)
End If
size = size + 1
End If
这是我的考试题目,vb做个atm却款机,为什么老说我下标越界?请教达人··
我试了,没用··晕,说的是这两句amount(size) = Val(txtDep.Text) Balance(size) = Balance(size - 1) + amount(size)下标越界··谁会啊··求教·

你的SIZE没设置初值,那么SIZE的初值为0,你的数组的下限是1,0当然就越界了! 所以你可以这样写
Size = Size + 1
amount(Size) = Val(txtdep.Text)
If Size = 1 Then
balance(Size) = amount(Size)
Else
balance(Size) = balance(Size - 1) + amount(Size)
End If
我已经调试过,没问题!

首先确定txtDep.Text中缺省是不是0,如果文本框不输内容进行Click,可能会有问题。

另外,f是什么类型,Text类型和f类型是否一致,
基本可靠方式是
f = val(txtDep.Text),把String强制为数值型。:)好久不碰vb了,写法上可能会有问题

--------------------------
另外size=? 进行合法性检查