The smallest piece of data a computer can handle is a bit, a single value that can be either 0 or 1. Eight bits are grouped into a byte. Computers typically measure disk space and memory space in kilobytes (1,024 bytes), megabytes (1,024 kilobytes), and gigabytes (1,024 megabytes).
Multiple bytes are grouped into words that may contain 2, 4, or more bytes depending on the computer hardware. Most computers these days use 4-byte (32-bit) words, although 8-byte (64-bit) computers are becoming more common.
Visual Basic also groups bytes in different ways to form data types with a higher logical meaning. For example, it uses 4 bytes to make an integer, a numeric data type that can hold values between ’2,147,483,648 and 2,147,483,647.
The following table summarizes Visual Basic’s elementary data types.
TYPE | SIZE | VALUES |
Boolean | 2 bytes | True or False |
Byte | 1 byte | 0 to 255 (unsigned byte) |
SByte | 1 byte | ’128 to 127 (signed byte) |
Char | 2 bytes | 0 to 65,535 (unsigned character) |
Short | 2 bytes | ’32,768 to 32,767 |
UShort | 2 bytes | 0 through 65,535 (unsigned short) |
Integer | 4 bytes | ’2,147,483,648 to 2,147,483,647 |
UInteger | 4 bytes | 0 through 4,294,967,295 (unsigned integer) |
Long | 8 bytes | ’9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
ULong | 8 bytes | 0 through 18,446,744,073,709,551,615 (unsigned long) |
Decimal | 16 bytes | 0 to +/’79,228,162,514,264,337,593,543,950,335 with no decimal point; 0 to +/’7.9228162514264337593543950335 with 28 places to the right of the decimal place |
Single | 4 bytes | ’3.4028235E+38 to ’1.401298E-45 (negative values) 1.401298E-45 to 3.4028235E+38 (positive values) |
Double | 8 bytes | ’1.79769313486231570E+308 to ’4.94065645841246544E-324 (negative values) 4.94065645841246544E-324 to 1.79769313486231570E+308 (positive values) |
String | varies | Depending on the platform, a string can hold approximately 0 to 2 billion Unicode characters |
Date | 8 bytes | January 1, 0001 0:0:00 to December 31, 9999 11:59:59 pm |
Object | 4 bytes | Points to any type of data |
Structure | varies | Structure members have their own ranges |
Signed types such as Integer and Decimal can store positive and negative numbers. Unsigned types such as Byte and UInteger can only store positive values and they use the extra space that would have been used to store sign information to store larger values.
Normally in a program you think of the Char data type as holding a single character. That could be a simple Roman letter or digit, but Visual Basic uses 2-byte Unicode characters so the Char type can also hold more complex characters from other alphabets such as Kanji and Cyrillic.
The System namespace also provides integer data types that specify their numbers of bits explicitly. For example, Int32 represents a 32-bit integer. Using these values instead of Integer emphasizes the fact that the variable uses 32 bits. That can sometimes make code clearer. For example, suppose that you need to call an application programming interface (API) function that takes a 32-bit integer as a parameter. You can make it obvious that you are using a 32-bit integer by giving the parameter the Int32 type.
The data types that explicitly give their sizes are Int16, Int32, Int64, UInt16, UInt32, and UInt64.
The Integer data type is usually the fastest of the integral types. You will generally get better performance using Integers than you will with the Char, Byte, Short, Long, or Decimal data types. You should stick with the Integer data type unless you need the extra range provided by Long and Decimal, or you need to save space with the smaller Char and Byte data types. In many cases, the space savings you will get using the Char and Byte data types isn’t worth the extra time and effort, unless you are working with a very large array of values.
Note that you cannot safely assume that a variable’s storage requirements are exactly the same as its size. In some cases, the program may move a variable so that it begins on a boundary that is natural for the hardware platform. For example, if you make a structure containing several Short (2-byte) variables, the program may insert 2 extra bytes between them so they can all start on 4-byte boundaries because that may be more efficient for the hardware. For more information on structures, see Chapter 23, “Classes and Structures.”
Some data types also come with some additional overhead. For example, an array stores some extra information about each of its dimensions.