2

I have this line:

    Dim l&
    l = 1000 * 60 '1 min

It throws an overflow error.

Why is that?

tmighty
  • 10,734
  • 21
  • 104
  • 218
  • Possible duplicate of https://stackoverflow.com/questions/5895816/vb6-overflow-error-with-large-integers – User51 Jan 14 '23 at 16:40
  • 1
    I don't think that's a perfect dupe as in this case the variable is defined as a long, the other case it was an integer. – StayOnTarget Jan 17 '23 at 11:10
  • 1
    Does this answer your question? [Overflow error for Long data type](https://stackoverflow.com/questions/44492345/overflow-error-for-long-data-type) – GSerg Jan 17 '23 at 11:12

1 Answers1

5

Vb's Help says:

You attempt to use a number in a calculation, and that number is coerced into an integer, but the result is larger than an integer. For example:

Dim x As Long
x = 2000 * 365   ' Error: Overflow

To work around this situation, type the number, like this:

Dim x As Long
x = CLng(2000) * 365
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
John Eason
  • 436
  • 2
  • 4
  • 3
    The abbreviated form `2000& * 365` also works, and can be used as well. The `&` coerces the number to be a long, and Long * Int is multiplied as long. – User51 Jan 14 '23 at 16:36
  • Yes. I know. I never ever used the short form in all the years I used VB from version 3 to 6! That was just a straight copy and paste from the MSDN Help. – John Eason Jan 14 '23 at 17:45
  • 2
    The short form is probably converted to a `Long` at compile time. The long form will almost certainly be converted to a `Long` at run time, i.e. it will run slightly slower, as well as the code being a little longer. – MarkJ Jan 16 '23 at 16:59