Visual Basic .NET is statically typed. Every type is a .NET type; the language gives short names to the primitive ones, and classes, structures, interfaces, enumerations, delegates and arrays are built on top. A value type (a structure or an enumeration, including the numeric types) holds its data in the variable and is copied on assignment; a reference type (a class, interface, array, delegate, String) holds a reference to an object.
The primitive types
| Keyword | .NET type | Holds |
|---|---|---|
Boolean | System.Boolean | True or False (converting True to a number gives −1 with CInt, 1 with Convert) |
Byte, SByte | Byte, SByte | 8-bit unsigned (0 to 255) and signed (−128 to 127) |
Short, UShort | Int16, UInt16 | 16-bit signed and unsigned |
Integer, UInteger | Int32, UInt32 | 32-bit signed and unsigned |
Long, ULong | Int64, UInt64 | 64-bit signed and unsigned |
Single, Double | Single, Double | 32-bit and 64-bit IEEE 754 floating point |
Decimal | System.Decimal | a 96-bit integer scaled by a power of ten (up to 28 digits after the point), for money |
Char | System.Char | one UTF-16 code unit |
String | System.String | an immutable sequence of UTF-16 code units |
Date | System.DateTime | a date and time of day, from 1 January of year 1 to the end of year 9999 |
Object | System.Object | any value; the root of every type |
The names of the types are keywords and are not case-sensitive. Variant is still reserved but is not a type; Object replaces it.
Declaring variables
A local variable is declared with Dim (or Static, Const, and Using or For for loop-scoped ones). The type follows As; without it the type is inferred from the initialiser (Option Infer) or is Object. In a list, each name takes the type of the As that follows it, so Dim c, d As Long makes both Long. As New T(args) creates the object in the declaration. A local is in scope from its declaration to the end of its block, and a name cannot be declared again in a nested block.
Dim a As Integer = 1, b As Integer = 2
Dim c, d As Long ' both Long
Dim e = 10 ' inferred Integer
Dim f As New List(Of String) ' constructor call
Dim h? As Integer = Nothing ' nullable
Dim i As Integer? = 5
Static calls As Integer ' keeps its value between calls
calls += 1
Const Pi As Double = 3.14159 ' a constant (also allowed at module and class level)
At type level a variable is a field: Private, Public, Protected, Friend, Protected Friend or Private Protected (version 15.5), optionally Shared (one per type), ReadOnly (assigned in the declaration or a constructor), WithEvents (see events) or Const. Dim at type level means Private in a class or module and Public in a structure; a field has an initialiser that runs when the object or type is created.
Nullable types, arrays and tuples
- Nullable value types:
Integer?(orNullable(Of Integer)) is an Integer that can also beNothing. Arithmetic on a nullable gives a nullable (null propagates), andIf(x, default)takes a default forNothing. The question mark may also follow the name:Dim h? As Integer. - Arrays:
Dim nums(9) As Integerdeclares an array with the upper bound 9, so ten elements, indexed from 0.Dim grid(2, 3) As Doubleis rectangular (3 by 4);Dim jagged()() As Integeris an array of arrays.New Integer() {1, 2}or the array literal{1, 2}creates one with values (the literal takes the dominant element type).ReDimchanges the size (ReDim Preservekeeps the contents of the last dimension) andEraseclears. - Tuples (version 15):
Dim t As (Integer, String) = (1, "one"); elements can be named,(Id:=7, Label:="seven"), and read ast.Item1or by name. The type isValueTuple.
Dim nums(9) As Integer ' ten elements, 0 to 9
Dim grid(2, 3) As Double ' 3 by 4
Dim jagged()() As Integer = New Integer(1)() {}
Dim lit = {1, 2, 3} ' array literal: Integer()
Dim names As String() = {"a", "b"}
ReDim Preserve nums(19)
Dim t As (Integer, String) = (1, "one")
Dim named = (Id:=7, Label:="seven")
Enumerations and structures
Enum Colour As Byte ' the underlying type (default Integer)
Red = 1
Green ' 2
Blue = 10
End Enum
<Flags>
Enum Access
None = 0
Read = 1
Write = 2
All = Read Or Write
End Enum
Structure Point
Public X As Integer
Public Y As Integer
Public Sub New(x As Integer, y As Integer)
Me.X = x
Me.Y = y
End Sub
End Structure
An enumeration has an integral underlying type, members with constant values (each is the previous plus one unless given) and no methods. A structure is a value type that can hold fields, properties, methods, events and constructors, implement interfaces, and cannot inherit (see classes).
Conversions
A widening conversion cannot lose information (Integer to Long, a class to its base class) and is implicit. A narrowing conversion can fail or lose data (Long to Integer, Object to anything, a base to a derived class); it is implicit only with Option Strict Off, and otherwise needs a conversion operator:
| Operator | Use |
|---|---|
CBool CByte CChar CDate CDbl CDec CInt CLng CObj CSByte CShort CSng CStr CUInt CULng CUShort | convert to a primitive type; CInt(3.7) rounds to 4 (to the nearest even on a half) |
CType(x, T) | convert to any type, using the conversions defined for it |
DirectCast(x, T) | a cast of a reference or an unbox, with no conversion; fails at run time if the object is not a T |
TryCast(x, T) | a reference cast that gives Nothing instead of failing |
TypeOf x Is T | tests the type (IsNot may follow, version 14) |
Dim n As Integer = CInt(3.7) ' rounds to 4
Dim s As String = CStr(n)
Dim o As Object = n
Dim back = DirectCast(o, Integer)
Dim m = TryCast(o, String) ' Nothing: o is not a String
Option Strict On.Not described
Known, not yet covered
- Reference: the tables of which conversion is widening and which narrowing between each pair of primitive types, the dominant-type rules, the exact rounding of every
C…operator and conversions through user-defined operators are in the conversions chapter of the specification and are not tabulated here. - Reference: the CLS and size details of the types (
Decimallayout,Dateas ticks) are given only in outline.
See also
- Operators and expressions – arithmetic, comparison and conversion in expressions
- Generics, lambdas and delegates – type parameters and delegate types