Results 1 to 10 of 10
  1. #1
    3 Star Lounger
    Join Date
    Jul 2001
    Posts
    283
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    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.
    Charlotte

  3. #3
    3 Star Lounger
    Join Date
    Jul 2001
    Posts
    283
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    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.
    Charlotte

  5. #5
    3 Star Lounger
    Join Date
    Jul 2001
    Posts
    283
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #6
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

    Legare Coleman

  7. #7
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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>
    Beryl M


  8. #8
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Flims, Switzerland
    Posts
    271
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #9
    3 Star Lounger
    Join Date
    Jul 2001
    Posts
    283
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
  •