Visual Basic 6 代码中的运行时溢出错误 [英] Runtime Overflow error in visual basic 6 code

查看:29
本文介绍了Visual Basic 6 代码中的运行时溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆