VB.NET Data Types
Data types in VB.NET and how variables are declared have changed since its predecessor, version 6.
VB.NET now supports class inheritance and can interoperate with other languages such as C# and C++.
In VB the Dim keyword declares variables. Dim is short for dimension.
Legal variable definitions in VB.NET
VB.NET data types
System.Object is the root of all types in the .NET Framework. The integer data types are byte, integer, short and long. The floating point data types are single, double and decimal.
Visual Basic type |
.NET Runtime type |
structure Storage size |
Value range |
Boolean |
Boolean |
Depends on implementing platform |
True or False |
Byte |
Byte |
1 byte |
0 to 255 (unsigned) |
Char |
Char |
2 bytes |
0 to 65535 (unsigned) |
Date |
DateTime |
8 bytes |
January 1, 1 CE to December 31, 9999 |
Decimal |
Decimal |
16 bytes |
+/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is +/-0.0000000000000000000000000001 |
Double |
Double |
8 bytes |
-1.79769313486231E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values |
Integer |
Int32 |
4 bytes |
-2,147,483,648 to 2,147,483,647 |
Long |
Int64 |
8 bytes |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Object |
Object (class) |
4 bytes on 32-bit platform |
Any type can be stored in a variable of type Object |
SByte |
1 byte |
-128 through 127 (signed) |
|
Short |
Int16 |
2 bytes |
-32,768 to 32,767 |
Single |
System.Single |
4 bytes |
-3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values |
String |
String (class) |
Depends on the implementing platform |
0 to approximately two billion Unicode characters |
UInteger |
UInt32 |
4 bytes |
0 through 4,294,967,295 (unsigned) |
ULong |
UInt64 |
8 bytes |
0 through 18,446,744,073,709,551,615 (1.8...E+19 †) (unsigned) |
UShort |
UInt16 |
2 bytes |
0 through 65,535 (unsigned) |
User-Defined Type (structure) |
(inherits from ValueType) |
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 |
As of VB.NET there is no longer a Currency data type. Use Decimal instead to represent currency amounts.
In .NET a TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The largest unit of time that the TimeSpan structure uses to measure duration is a day.
VB.NET supports type declaration characters for numeric literals. They are not required in the code but it is mentioned here for completeness and for awareness that earlier VB code may use them.
Other examples:
http://msdn.microsoft.com/en-ca/library/s9cz43ek(v=vs.100).aspx
Hexadecimal values can be expressed with a preceding &H as in &H100, &haa, &h00ff which represent in decimal the values 256, 170, and 255 respectively.
VB.NET provides the function Hex(n) which converts the n parameter from hex value to a decimal value. If the parameter is not a number, then an InvalidCastException is thrown.
Octal values are represented with the &O prefix. The Oct(n) function converts a supplied octal value into its decimal equivalent. A valid octal value uses only the digits 0-7.
Char values are assigned with a single character enclosed in double quotes, optionally followed by a lowercase or uppercase C as in
Dim chrSymbol As Char = "a"c
If the String has more than one character, only the first one is assigned to the Char variable.
In VB6 the data type Variant served as the universal data type capable of holding any kind of object value. In VB7 variants no longer exist -- Object is the VB7 version of Variant.
Each data type has an associated type code which you can discover with the Type class's GetTypeCode method to obtain the TypeCode number.
In VB6 it was optional to explicitly declare a variable before using it. In VB.NET that is no longer the case. The Option Explicit feature is imposed by default which means variables have to be declared before they are used. This option is found in Visual Studio under the Tools Menu.
Option Compare is used to define how strings are to be compared either by text (case insensitive) or by binary (case sensitive and based on locale character set). For example you may require that both 'a' and 'A' are sorted before 'b' and 'B' in your application code, in which case you would need to set Option Compare Text. The default is binary. http://msdn.microsoft.com/en-us/library/8t3khw5f.aspx
In Visual Studio 2008 Option Infer was added to allow for compatibility to older VB code that used a data type inference as in below. The default is Option Infer On.