佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1009|回复: 4

vb6 费解的问题

[复制链接]
发表于 20-1-2009 12:19 PM | 显示全部楼层 |阅读模式
我用了vb6 有 n年了,今天才发现这个问题:

Dim a As Long

a = 20000 + 20000

error "Overflow" 会出现。
有谁知道是什么问题吗?
回复

使用道具 举报


ADVERTISEMENT

发表于 21-1-2009 07:11 PM | 显示全部楼层
试试 a=32768 + 2

如果一样的问题,那应该是 32bits limit for Long type
回复

使用道具 举报

发表于 21-1-2009 07:40 PM | 显示全部楼层
  1. a = 20000 + 20000&
复制代码
or
  1. a = 20000 + CLng(20000)
复制代码
就没问题
回复

使用道具 举报

发表于 25-1-2009 01:32 PM | 显示全部楼层
这好像是vb6的bug。。。

如果你的计算中,第一和第二或者之后的都是integer type(16 bits),那么计算结果也是integer type。。。所以,20000 + 20000 = 40000,就出现overflow。。。最后,vb 返回给 a 的答案就是 overflow。。。
  1. Private Sub Form_Load()

  2. On Error GoTo myerror
  3. Dim a As Long

  4. a = 32767 + 1 '= 32768 (+ve integer limit, overflow error)

  5. a = 32768 + 1 '= 32769 (ok, because the 1st parameter already long type)
  6. a = 32767& + 1 '= 32768 (ok, because we force the 1st parameter to long type)
  7. a = 32767 + CLng(1) '= 32768 (ok, because we force the 2nd parameter to long type)

  8. a = 32766 - 1 + 2  '(ok)
  9. a = 32766 + 2 - 1  'Overflow error
  10. 'Overflow, because 32766 + 2 > 32767, even
  11. 'the above calculation also result in 32767

  12. Exit Sub
  13. myerror:

  14. MsgBox "Runtime error!!!, " & Err.Description
  15. Resume Next

  16. End Sub
复制代码
回复

使用道具 举报

发表于 11-2-2009 01:25 PM | 显示全部楼层

回复 4# meemee 的帖子

应该java也是这样,
因为程式

1。A = B + C
    是重右边运行起,然后存到左边要符合
    Integer = Integer
    Long = Long

2。数字全部默认为Integer (如果少过32767)

3。当Integer + Integer = Integer
    当Integer + Long = Long (或相反)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 16-5-2024 12:34 PM , Processed in 0.059175 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表