For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > November 2004 > Question about Long calculation









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Question about Long calculation
Michael Bosco

2004-11-26, 8:55 pm

Hi. I'm seeing some weird behavior when doing a simple calculation in
VB6. I have a Long defined like so...
Dim lngNumber As Long

This line works fine...
lngNumber = 56000

However, this line gives me an overfow error...
lngNumber = (56 * 1000)

How is the second line different from the first??

Thanks.
Rick Rothstein

2004-11-26, 8:55 pm

> Hi. I'm seeing some weird behavior when doing a simple calculation in
> VB6. I have a Long defined like so...
> Dim lngNumber As Long
>
> This line works fine...
> lngNumber = 56000
>
> However, this line gives me an overfow error...
> lngNumber = (56 * 1000)
>
> How is the second line different from the first??


VB has what I consider a strange quirk (a bug in my opinion)... for
non-floating point multiplication, VB uses the smallest memory size it
can get away with. It determines this by looking at the size of the
numbers it is going to multiply together and (here is the part I think
is buggy) not by looking at the size of the variable the answer will be
stored in. Since 56 and 1000 will BOTH fit into an Integer sized memory
area, VB attempts to store the answer in an Integer sized memory area
(paying no attention to the data type of the lngNumber variable). But
the product, 56000, is too large to fit and so VB throws an error.
However, if you force one (or both) of these to numbers to a Long
(either by tacking on the ampersand (&) suffix or by using the CLng
function), then the calculation would work. Either...

lngNumber = 56& * 1000

or

lngNumber = CLng(56) * 1000

Rick - MVP

Gerald Hernandez

2004-11-26, 8:55 pm

VB's default casting of 56 and 1000 are Integer (Int16).
You need to tell VB that these should be treated as Longs (Int32)
You can do this by appending the Long datatype decorator & like so:
lntNumber = (56& * 1000&)

Gerald

"Michael Bosco" <mbosco51@hotmail.com> wrote in message
news:f1ba6018.0411261241.14dc16a2@posting.google.com...
> Hi. I'm seeing some weird behavior when doing a simple calculation in
> VB6. I have a Long defined like so...
> Dim lngNumber As Long
>
> This line works fine...
> lngNumber = 56000
>
> However, this line gives me an overfow error...
> lngNumber = (56 * 1000)
>
> How is the second line different from the first??
>
> Thanks.



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com