More Variables Types
What will be printed on the form after executing the following
code?
Dim ABC As Integer
ABC = 4.8
Print ABC
Answer:
The Integer variable can't store a number with fraction, So the
computer will round the 4.8 to 5, and will Insert 5 to the ABC
variable.
So the Command Print ABC will print 5 on
the form.
So how can you store a number with fraction in a variable?
For this purpose you have other variable types that can store round
numbers like the Integer type, and in addition can store
numbers with fractions.
The Variable types list:
Byte - Can store only Integer numbers
between 0 to 255
Integer - Can store only Integer
numbers between -32,768 to 32,767
Long - Can store only Integer numbers
between -2,147,483,648 to 2,147,483,647
Single - Can store Non-Integer (numbers
with fractions) Numbers between -3.402823E38 to -1.401298E-45
for negative values, and 1.401298E-45 to 3.402823E38 for positive
values.
Double - Can store Non-Integer Numbers between
-1.79769313486231E308 to -4.94065645841247E-324 for negative values,
and 4.94065645841247E-324 to 1.79769313486232E308 for positive
values
Why would I use Byte variable that can store Integer numbers between
0 to 255, while I can use Integer variable that can store numbers
between -32,768 to 32,767?
The Answer is: Because the Byte variable
is taking less memory.
So if you have variable that will not store numbers greater
than 255 or less than 0, declare it as Byte variable
and save a little amount of memory.
If you need to store Non-Integer numbers in variable,declare it
as Single or as Double.
The Declaring command
is very simple:
Dim Abc As Double
Dim Blah As Single
Dim Popeye As Byte
And so on.