Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 There's a mistake in 5 simple lines!
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HazySmoke)345




PostPosted: Thu Nov 10, 2005 8:30 pm   Post subject: There's a mistake in 5 simple lines!

code:
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?
Sponsor
Sponsor
Sponsor
sponsor
GlobeTrotter




PostPosted: Thu Nov 10, 2005 8:35 pm   Post subject: (No subject)

You forgot to finish your sentence.

It should be "Dim a Integer"
HazySmoke)345




PostPosted: Thu Nov 10, 2005 9:16 pm   Post subject: (No 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.

code:
Private Sub Form_Load()
    Dim a As Long
    a = 15625 * 15
    Print a
End Sub


And it still overflows.
GlobeTrotter




PostPosted: Thu Nov 10, 2005 10:04 pm   Post subject: (No subject)

I think I've figured it out.

Quote:
Private Sub Form_Load()
Print 32768 + 1
End Sub


that works, while

code:
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




PostPosted: Fri Nov 11, 2005 2:27 pm   Post subject: (No 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
Brightguy




PostPosted: 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):

VisualBASIC:
Print 15625& * 15

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.
HazySmoke)345




PostPosted: Fri Nov 11, 2005 5:09 pm   Post subject: (No 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:

code:
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




PostPosted: Fri Nov 11, 2005 6:46 pm   Post subject: (No subject)

And people say VB isn't a great language. Wacky people...
Sponsor
Sponsor
Sponsor
sponsor
Brightguy




PostPosted: 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...

Laughing
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: