It's time to create a new variable—but what type should you use? For that matter, exactly what kinds of variable types are there and what do they do? Even if you remember what types there are, you probably won't remember the range of possible values a particular variable type allows.
There is a wide range of data types. The Visual Basic variable types appear in Table 2.4 for reference, making selecting the right type a little easier. Note that the Single and Double types handle floating point values, which the Integer types (such as Short, Integer, and Long) do not; these names are short for single precision floating point and double precision floating point. You might also notice that there are some new types in VB .NET that weren't in VB6, like Char, and that some other types, like Currency or Variant, are gone. Note in particular the Boolean data type, which takes values like True or False only—also called logical values.
Type |
Storage size |
Value range |
---|---|---|
Boolean |
2 bytes |
True or False |
Byte |
1 byte |
0 to 255 (unsigned) |
Char |
2 bytes |
0 to 65535 (unsigned) |
Date |
8 bytes |
January 1, 0001 to December 31, 9999 |
Decimal |
16 bytes |
+/-79,228,162,514,264,337,593,543,950,335 with no decimal point; 7.92281625142643 37593543950335 with 28 places to the right of the decimal; smallest non-zero number is 0.00000 00000000000000000000001 |
Double |
8 bytes |
-1.79769313486231E+308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486231E+308 for positive values |
Integer |
4 bytes |
-2,147,483,648 to 2,147,483,647 |
Long |
8 bytes |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Object |
4 bytes |
Any type can be stored in a variable of type Object |
Short |
2 bytes |
-32,768 to 32,767 |
Single |
4 bytes |
-3.402823E to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E for positive values |
String |
Depends on implementing platform |
0 to approximately 2 billion Unicode characters |
User-Defined Type (structure) |
Sum of the sizes of its members. Each member of the structure has a range determined by its data type and independent of the ranges of the other members |
Tip |
To see an example using the Date data type, see the topic "Handling Dates and Times" in this chapter. |