VB6 overflow error with large integers

你离开我真会死。 提交于 2019-12-09 07:50:02

问题


I am trying to set an integer value as such:

Dim intID as integer
intID = x * 10000

This works ok when x is 3 or less. But when x is 4, this gives me the error:

run-time error 6 Overflow

I don't understand why this is. I can set intID to 40000 directly without any problems, so it's obviously capable of storing large numbers.


回答1:


You *cannot set a vb6 integer to 40000 as they are signed 16 bit numbers so +32767 is the maximum.

Long is the 32 bit type.

However as a caveat, if you were to:

Dim lngID As Long
lngID = 4 * 10000

You would still get an overflow as literal numbers default to Integer, to correct that just type one as long with & or cast one as long using CLng():

Dim lngID As Long
lngID = 4 * 10000&
lngID = 4 * CLng(10000)

Update:




回答2:


in VB6, the Integer type is a whole number which ranges from -32768 to 32767.

You would be best using the Long type here.




回答3:


In VB Integer variable range is -32,768 to 32,767 If any variable value is more than this range in your program,you have to declare Data type Long instead of Integer.

Dim intID as integer
intID = x * 10000

Dim lngID AS Long

lngID = x * CLng(10000)
' if 10000
' whatever you want to be


来源:https://stackoverflow.com/questions/5895816/vb6-overflow-error-with-large-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!