
-----------------------------------
HazySmoke)345
Thu Nov 10, 2005 8:30 pm

There's a mistake in 5 simple lines!
-----------------------------------
Private Sub Form_Load()
    Dim a
    a = 15625 * 15
    Print a
End Sub

And guess what happens when I run the program? It says that it has overflown! But it can't have, can it? That's not a big number, and when I changed the 3rd line to "a = 88888 * 88", it works. Is it just me? Can anyone show me what's wrong?

-----------------------------------
GlobeTrotter
Thu Nov 10, 2005 8:35 pm


-----------------------------------
You forgot to finish your sentence.

It should be "Dim a Integer"

-----------------------------------
HazySmoke)345
Thu Nov 10, 2005 9:16 pm


-----------------------------------
I don't really think it matters if I state what kind of variable it is... Okay, I rewrote the script like this, simply stating the type of variable.

Private Sub Form_Load()
    Dim a As Long
    a = 15625 * 15
    Print a
End Sub


And it still overflows.

-----------------------------------
GlobeTrotter
Thu Nov 10, 2005 10:04 pm


-----------------------------------
I think I've figured it out.

Private Sub Form_Load()
    Print 32768 + 1
End Sub

that works, while

Private Sub Form_Load()
    Print 32767 + 1
End Sub

doesn't work.  32767 is the maximum value for an integer.  Thus, VB probably sees the first number, assumes the answer will be an integer, and when it isn't, it crashes.

-----------------------------------
pavol
Fri Nov 11, 2005 2:27 pm


-----------------------------------
that shouldn't be the problem if he's declared a as a long, since long's max nuber is somewhere in the billions. but i do see a problem in the fact that nothing will actually show up on the form since print is the form_load procedure. you should try putting it in form_activate or in a command_click procedure. 
just a thought

-----------------------------------
Brightguy
Fri Nov 11, 2005 3:32 pm

Re: There's a mistake in 5 simple lines!
-----------------------------------
Like you discovered, the problem is that you are multiplying two Integer data types, and the result returned is an Integer as well, which is over the limit.  I thought VB would have scaled that up to a Long, though...  But just manually specify a data type to overcome the problem (for example, & is the symbol for Long):

Print 15625& * 15
but i do see a problem in the fact that nothing will actually show up on the form since print is the form_load procedure.
The text is printed on the form before it is drawn on the screen, consequently if you want to see it set AutoRedraw to True.

-----------------------------------
HazySmoke)345
Fri Nov 11, 2005 5:09 pm


-----------------------------------
Like you discovered, the problem is that you are multiplying two Integer data types, and the result returned is an Integer as well

Well, I rewrote the script again, like this:

Private Sub Form_Load()
    Dim a As Long
    a = Val(15625) * 15
    Print a
End Sub

Now, I don't really see a difference between 15625 and Val(15625). The Val one works, the original one doesn't... What kind of data type does the computer assume if I put Val() around it?

And the "&" sign works, thanks.

-----------------------------------
wtd
Fri Nov 11, 2005 6:46 pm


-----------------------------------
And people say VB isn't a great language.  Wacky people...

-----------------------------------
Brightguy
Sat Nov 12, 2005 2:01 am

Re: There's a mistake in 5 simple lines!
-----------------------------------
Now, I don't really see a difference between 15625 and Val(15625). The Val one works, the original one doesn't... What kind of data type does the computer assume if I put Val() around it?
15625 is assumed to be an integer since it is in the correct range (-2^15 to 2^15-1).  Val(15625) is a Double since that's the type Val() returns.

And people say VB isn't a great language.  Wacky people...
 :lol:
