Longs

.net , vb6 United Kingdom
  • 18 years ago

    I am trying to multiply 255 by 255 and storing that into a long variable. I have tried many different things but I keep on getting the same error, Overflow. That large of a number should easily be abled to be stored in a long. This is the current code that does not work.


    Dim num As Long


    num = 255 * 255


    Very, very simple but it doesn't work! Could anybody help me. Thanks,


    Alex

  • 18 years ago

    Doesn't work on my computer, thats strange. All i can suggest is that you either use a string and input the result into the long variable, i.e.:


    Code:

    dim sNum as string
    dim Num as long


    sNum = "255" * "255"
    Num = cLng(sNum)



    or if its appropriate, square the value i.e.:


    Code:

    dim Num as long


    Num = 255^2



    Hope that helps.

  • 18 years ago

    That is strange. Though I was able to do it like this


    Dim X As Long
       X = 255
       X = X * X


    and like this


    Dim X As Long
       X = CLng(255) * CLng(255)

  • 18 years ago

    Why don't you just do this:


    Code:
    Dim X As Long


    X = 65025   ' 255 * 255 = 65025



    It's better to do your calculations on constants before runtime...

  • 18 years ago

    Hmmm... I get an overflow error as well.. thats is really wierd. However heres a temporary work around:


    Code:

    Dim num As Long
    num = 255
    num = num * 255
    MsgBox num


    Hope that helps!

  • 18 years ago

    Thanks for the code that set the variable to a number and then multiplied the number by itself. That worked. I still don't really know why you can't do what I tried.


    Alex

  • 18 years ago

    well considering that computers don't multiply in decimal, it could have something to do with the binary mulitplication algorithm.

  • 18 years ago

    well alex if it was my code your talking about(im a little which your talking about) its OK! I inquired with a VB Developer(from the MS Visual Basic Developer Team) and he tells me its an odd occurance. Unfortunetly it may not be corrected because SP5 was the last SP for teh VS6 series, now its .NET all the way!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Better train people and risk they leave – than do nothing and risk they stay.” - Anonymous