|
-
May 4th, 2000, 05:41 PM
#1
Thread Starter
Member
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
-
May 4th, 2000, 06:03 PM
#2
Thread Starter
Member
Ok boss
But
tell me the reason why mine is not working
-
May 4th, 2000, 06:37 PM
#3
Conquistador
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
-
May 4th, 2000, 06:40 PM
#4
Conquistador
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
-
May 4th, 2000, 06:54 PM
#5
Lively Member
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)
-
May 4th, 2000, 07:00 PM
#6
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&
-
May 4th, 2000, 07:18 PM
#7
Conquistador
what's the matter with my way?
-
May 4th, 2000, 07:56 PM
#8
Thread Starter
Member
Thanks Nina. You cleared why it is not working.
and Thanks to Frans C for giving correct solution
-
May 4th, 2000, 09:39 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|