The following table summarizes the elementary data types of Visual Basic.
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 |
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 | variable | 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 | variable | Structure members have their own ranges |
The System namespace also provides integer data types that specify their number 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. In Visual Basic 6, a Long uses 32 bits but in Visual Basic .NET, an Integer uses 32 bits. 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.
Some data types also come with some additional overhead. For example, an array stores some extra information about each of its dimensions.