# Thread: VB6 long int var (VB6)

1. ## VB6 long int var (VB6)

I have written the following simple program to test out the long integer type variable:

Private Sub Command1_Click()
Dim lngValue As Long
lngValue = 330 * 100
MsgBox lngValue
End Sub

It however always bombs, saying overflow. I wonder why a long integer variable can accept only a small value.

Thanks

2. ## Re: VB6 long int var (VB6)

If you want a guess, it's interpreting the numeric values as doubles or variants. If you explicitly assign each of those values to a long variable and then multiply the two variables instead of the values themselves, you won't get an overflow error.

3. ## Re: VB6 long int var (VB6)

Yes Charlotte, you are right. But that would be strange though. For example, if I have the following:

Dim intA As Integer
Dim intB As Integer
Dim lngC As Long
intA = 100
intB = 330
lngC = intA * intB
MsgBox lngC

it would not work because it is expecting an integer for lngC not long integer.

Thanks again.

4. ## Re: VB6 long int var (VB6)

Actually, that should work because you can always stuff a smaller datatype into a larger one, but a double is bigger than a long, so it doesn't always coerce down.

5. ## Re: VB6 long int var (VB6)

I did try that and it bombs, point to the overflow problem with

lngC = intA * intB

Since both A and B are defined as integer, the program somehow expect lngC is also an integer. A value of the product A * B that is more than what an integer type variable can take will cause an error.

If I define both A and B as long integer, the program works perfectly.

Very strange but thanks a lot.

6. ## Re: VB6 long int var (VB6)

That is happening because VB looks at the 330 and 100 and decides that they are interger types. It then creates an interger intermediate variable to store the result of the multiplication, and the result overflows that intermediate variable. The conversion to long happens after the multiplication and assignment to the intermediate variable. The following code shows two ways around that:

<pre>Dim lngValue As Long
lngValue = 330& * 100&
MsgBox lngValue
lngValue = CLng(330) * CLng(100)
MsgBox lngValue
</pre>

7. ## Re: VB6 long int var (VB6)

Alternatively just put the figures in double quotes so it sees them as figures, ie:

Private Sub Command1_Click()
Dim lngValue As Long
lngValue = "330" * "100"
MsgBox lngValue
End Sub

<img src=/S/smile.gif border=0 alt=smile width=15 height=15>

8. ## Re: VB6 long int var (VB6)

I wouldn't recommend that. That means an implicit cast from a string to a double, and then a floating point operation. This would be highly inefficient compared to explicit casting from integer to Long. I'd follow LegareColeman's recommendation of
<pre>lngValue = CLng(330) * CLng(100)</pre>

9. ## Re: VB6 long int var (VB6)

Here is an workable alternative:

dim intA as integer
dim intB as integer
intA = val(text1)
intB = val(text2)
text3 = val(intA) * val(intB)

'assuming that you enter two numeric values into textboxes text1 and text2. The strange thing is that
'you need to use the val function again in the computation. It works though.

10. ## Re: VB6 long int var (VB6)

As LC points out, there is a workaround.

This is die to the "evil of type coercion" FEATURE in VB/VBA.

Hope that this gets fixed in VB7 and C#.

Use the conversion functions suxh as CLng.
Don;t use the abomination & to denote a data type.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•