Results 1 to 9 of 9

Thread: Tell me the reason

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2000
    Posts
    55
    If i run the following code it gives an error. why?

    Private Sub Command1_click()

    Dim b as Long

    b=30*4000

    MsgBox b

    End Sub

  2. #2

    Thread Starter
    Member
    Join Date
    Mar 2000
    Posts
    55

    Red face

    Ok boss

    But
    tell me the reason why mine is not working

  3. #3
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    or u could use the integer declaration
    Code:
    Dim a As Integer, b As Integer, c As Integer
    a = 30
    b = 4000
    c = a * b
    MsgBox c
    it does not work, because the value of which you are trying the set to the "long" variable, is bigger than the maximum alllowed size

  4. #4
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    oh yeah, you can do it this way too:
    Code:
    Dim x As Long
    x = CLng(4000) * 30
    MsgBox x


    it's shorter than the other methods

  5. #5
    Lively Member
    Join Date
    Mar 2000
    Location
    Germany
    Posts
    84
    you got a lot of quite confused with your problem, but one at my office gave me the reason

    Dim b as Long
    b=30*4000

    you try to multiply two constants,
    Visual Basic uses the smallest datatype for each constant, which is Integer, for calculating the result
    that's the reason why 30*1092=32670 works, and 30*1093=32790 gives you an overflow, because it's bigger than the maximum Integer(32768)

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I consider it good practice to specify the datatype of constants. You don't get into these problems if you do this.
    eg
    dim x as long
    x = 30& * 4000&

  7. #7
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    what's the matter with my way?

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2000
    Posts
    55

    Smile

    Thanks Nina. You cleared why it is not working.

    and Thanks to Frans C for giving correct solution

  9. #9
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    da_silvy,

    Nothing is wrong with your way, I just wanted to show an alternative.

Posting Permissions

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



Click Here to Expand Forum to Full Width