Computer Science Canada There's a mistake in 5 simple lines! |
Author: | HazySmoke)345 [ Thu Nov 10, 2005 8:30 pm ] | ||
Post subject: | There's a mistake in 5 simple lines! | ||
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? |
Author: | GlobeTrotter [ Thu Nov 10, 2005 8:35 pm ] |
Post subject: | |
You forgot to finish your sentence. It should be "Dim a Integer" |
Author: | HazySmoke)345 [ Thu Nov 10, 2005 9:16 pm ] | ||
Post subject: | |||
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.
And it still overflows. |
Author: | GlobeTrotter [ Thu Nov 10, 2005 10:04 pm ] | ||
Post subject: | |||
I think I've figured it out. Quote: Private Sub Form_Load()
Print 32768 + 1 End Sub that works, while
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. |
Author: | pavol [ Fri Nov 11, 2005 2:27 pm ] |
Post subject: | |
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 |
Author: | Brightguy [ Fri Nov 11, 2005 3:32 pm ] | ||
Post subject: | 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):
pavol wrote: 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. |
Author: | HazySmoke)345 [ Fri Nov 11, 2005 5:09 pm ] | ||
Post subject: | |||
Quote: 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:
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. |
Author: | wtd [ Fri Nov 11, 2005 6:46 pm ] |
Post subject: | |
And people say VB isn't a great language. Wacky people... |
Author: | Brightguy [ Sat Nov 12, 2005 2:01 am ] |
Post subject: | Re: There's a mistake in 5 simple lines! |
HazySmoke)345 wrote: 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. wtd wrote: And people say VB isn't a great language. Wacky people...
|