Visual Basic 6 代码中的运行时溢出错误 [英] Runtime Overflow error in visual basic 6 code
问题描述
以下 Visual Basic 6.0 - SP2 代码出现溢出错误.有人能解释一下原因吗?
Following code of Visual Basic 6.0 - SP2 is giving Overflow error. Can somebody explain why?
Private Sub Form_Click()
Dim Qty as Long
Qty= 290 * 113 '' 112 is working fine
MsgBox Qty
End Sub
推荐答案
表达式的类型由其成员决定,而不是由它要存储的变量决定.
The type of an expression is determined by its members, not by the variable it is going to be stored in.
113
被输入为 Byte
.290
被输入为 Integer
因为它不能放入一个字节中.
113
gets typed as Byte
.
290
gets typed as Integer
because it won't fit into a byte.
由于涉及的最大类型是Integer
,所以整个表达式290 * 113
的类型为Integer
.一个Integer
最多可以包含32767
,小于290 * 113
.
As the largest of the involved types is Integer
, the entire expression 290 * 113
is typed as Integer
. An Integer
can contain at most 32767
, which is less than 290 * 113
.
因此它在乘法时溢出,在结果存储到 Long
变量之前.
It therefore overflows upon multiplication, before the result is stored into a Long
variable.
明确输入至少一个数字为 Long
:
Explicitly type at least one of the numbers as Long
:
Qty = 290& * 113
这篇关于Visual Basic 6 代码中的运行时溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!