Post by Ildûrest on Mar 10, 2005 9:43:41 GMT
There are five main datatypes in QB. Integer, Long integer, Single precision, Double precision, and, of course, STRING.
A datatype is simply the way a value is treated. Here's an analogy: a circle can be treated as the letter O, a zero, or a shape. What you decide it is will determine how you treat it.
It is somewhat similar when programming. In QB almost all the datatype work is done for you (however that doesn't stop you from taking more control once you learn how). So:
-Integers are very fast to PRINT and operate (+ - * / etc). Their range is -32 768 to 32 767. They cannot store fractional values.
-Long integers are a little bit slower, but still relatively fast. Their range is -2,147,483,648 to 2,147,483,647. They can also not store fractional values.
-Single and Double precision have a movable decimal point. That makes them very slow compared with both integer types. Double precision is even slower than Single precision. But they can store precise, or large, values.
Singles range from 2.802597*10^-45 to 3.402823*10^38 both in positive and negative values.
Doubles have an amazingly enormous range which I won't bother putting here.
However, although the decimal point is movable, the number of digits possible is still fixed. This means numbers can't be both very large, and very accurate at the same time, they can either be one or the other.
(With integers, of course, this problem does not occur, although the ranges aren't as big)
An integer uses 16 bits, that is, two bytes. A long-integer uses four bytes. A single-precision value uses four bytes, and the double-precision number uses a whopping eight bytes.
Strings, on the other hand, use one byte for each character. They're really just arrays in disguise.
Suffixes are as follows:
Integer - %
Long Integer - &
Single - !
Double - #
(String - $)
A variable name ending in one of those is of that type. E.g. A# is a double-precision value, A$ is a string.
A variable can also be declared. The main way is
DIM VarName AS datatype
E.g.
DIM A AS INTEGER
DIM B AS LONG
DIM C AS SINGLE
DIM D AS DOUBLE
DIM E AS STRING
DIM F AS STRING * 5
(The STRING * 5 means F is a fixed-length string, whose length is always 5. If you put something longer in it, it gets cut off, if you put something shorter in it, the rest turns to spaces. These are faster than normal strings.)
In SUBs you can declare variables that won't be erased when you run the SUB the next time using
STATIC VarName AS type
You can also declare constants using
CONST Varname%
Where you replace the % with the suffix you want. This suffix isn't included in the variable name later on in the program.
---OTHERWISE---
If the variable has no suffix, and is not declared, it will by default be Single-Precision. However you can change the defaults, using
DEFINT A-Z
DEFLNG A-B
DEFSNG G-H
DEFDBL L-P
DEFSTR S-T
(DEFSTR is string)
The two letters after the DEFxxx statement are the first letters of the variable names that this will apply to. I.e. DEFINT A-F means any undeclared variable with no suffix starting with A, B, C, D, E or F will be an Integer.
That's about all. I recommend you ONLY use integers unless you have a very, very good reason to use a floating-point number.
A datatype is simply the way a value is treated. Here's an analogy: a circle can be treated as the letter O, a zero, or a shape. What you decide it is will determine how you treat it.
It is somewhat similar when programming. In QB almost all the datatype work is done for you (however that doesn't stop you from taking more control once you learn how). So:
-Integers are very fast to PRINT and operate (+ - * / etc). Their range is -32 768 to 32 767. They cannot store fractional values.
-Long integers are a little bit slower, but still relatively fast. Their range is -2,147,483,648 to 2,147,483,647. They can also not store fractional values.
-Single and Double precision have a movable decimal point. That makes them very slow compared with both integer types. Double precision is even slower than Single precision. But they can store precise, or large, values.
Singles range from 2.802597*10^-45 to 3.402823*10^38 both in positive and negative values.
Doubles have an amazingly enormous range which I won't bother putting here.
However, although the decimal point is movable, the number of digits possible is still fixed. This means numbers can't be both very large, and very accurate at the same time, they can either be one or the other.
(With integers, of course, this problem does not occur, although the ranges aren't as big)
An integer uses 16 bits, that is, two bytes. A long-integer uses four bytes. A single-precision value uses four bytes, and the double-precision number uses a whopping eight bytes.
Strings, on the other hand, use one byte for each character. They're really just arrays in disguise.
Suffixes are as follows:
Integer - %
Long Integer - &
Single - !
Double - #
(String - $)
A variable name ending in one of those is of that type. E.g. A# is a double-precision value, A$ is a string.
A variable can also be declared. The main way is
DIM VarName AS datatype
E.g.
DIM A AS INTEGER
DIM B AS LONG
DIM C AS SINGLE
DIM D AS DOUBLE
DIM E AS STRING
DIM F AS STRING * 5
(The STRING * 5 means F is a fixed-length string, whose length is always 5. If you put something longer in it, it gets cut off, if you put something shorter in it, the rest turns to spaces. These are faster than normal strings.)
In SUBs you can declare variables that won't be erased when you run the SUB the next time using
STATIC VarName AS type
You can also declare constants using
CONST Varname%
Where you replace the % with the suffix you want. This suffix isn't included in the variable name later on in the program.
---OTHERWISE---
If the variable has no suffix, and is not declared, it will by default be Single-Precision. However you can change the defaults, using
DEFINT A-Z
DEFLNG A-B
DEFSNG G-H
DEFDBL L-P
DEFSTR S-T
(DEFSTR is string)
The two letters after the DEFxxx statement are the first letters of the variable names that this will apply to. I.e. DEFINT A-F means any undeclared variable with no suffix starting with A, B, C, D, E or F will be an Integer.
That's about all. I recommend you ONLY use integers unless you have a very, very good reason to use a floating-point number.