Reputation: 10433
May be its very basic and sound like a pretty stupid question. But I am confused with this. So, when I try to do 1000 * 1000 in a double variable I get an overflow error. Where as for I keep on multiplying the variable it self, it works fine. Can some one please explain, why?
Sub test()
Dim x As Double
x = 1000 * 1000 ' ~~>> Give OverFlow Error. WHY ?
x=1000
x = x * x * x * x ' ~~>> Works Fine
MsgBox x
End Sub
Upvotes: 2
Views: 82
Reputation: 4838
Ah. VBA how I love thee.
In this case, it is treating the 1000 as an integer, which in VB6 is 16-bit(max value of 32767), so it is overflowing because 1 million is too large to fit in an integer.
You can "cast" the literal to a specific type by using terrible type characters.
Full list here. Yes, the feature is preserved in .NET. https://msdn.microsoft.com/en-us/library/s9cz43ek.aspx
Sub test()
Dim x As Double
x = 1000& * 1000& ' Long literals
x = 1000
x = x * x * x * x ' ~~>> Works Fine
MsgBox x
End Sub
Upvotes: 3