Short data type (Visual Basic)
Holds signed 16-bit (2-byte) integers that range in value from -32,768 through 32,767.
Short data type to contain integer values that do not require the full data width of
Integer. In some cases, the common language runtime can pack your
Short variables closely together and save memory consumption.
The default value of
Short is 0.
You can declare and initialize a
Short variable by assigning it a decimal literal, a hexadecimal literal, an octal literal, or (starting with Visual Basic 2017) a binary literal. If the integer literal is outside the range of
Short (that is, if it is less than Int16.MinValue or greater than Int16.MaxValue, a compilation error occurs.
In the following example, integers equal to 1,034 that are represented as decimal, hexadecimal, and binary literals are implicitly converted from Integer to
Dim shortValue1 As Short = 1034 Console.WriteLine(shortValue1) Dim shortValue2 As Short = &H040A Console.WriteLine(shortValue2) Dim shortValue3 As Short = &B0100_00001010 Console.WriteLine(shortValue3) ' The example displays the following output: ' 1034 ' 1034 ' 1034
You use the prefix
&H to denote a hexadecimal literal, the prefix
&B to denote a binary literal, and the prefix
&O to denote an octal literal. Decimal literals have no prefix.
Starting with Visual Basic 2017, you can also use the underscore character,
_, as a digit separator to enhance readability, as the following example shows.
Dim shortValue1 As Short = 1_034 Console.WriteLine(shortValue1) Dim shortValue3 As Short = &B00000100_00001010 Console.WriteLine(shortValue3) ' The example displays the following output: ' 1034 ' 1034
Starting with Visual Basic 15.5, you can also use the underscore character (
_) as a leading separator between the prefix and the hexadecimal, binary, or octal digits. For example:
Dim number As Short = &H_3264
To use the underscore character as a leading separator, you must add the following element to your Visual Basic project (*.vbproj) file:
<PropertyGroup> <LangVersion>15.5</LangVersion> </PropertyGroup>
For more information see setting the Visual Basic language version.
Numeric literals can also include the
S type character to denote the
Short data type, as the following example shows.
Dim number = &H_3264S
Shortdata type widens to
Double. This means you can convert
Shortto any one of these types without encountering a System.OverflowException error.
Type Characters. Appending the literal type character
Sto a literal forces it to the
Shorthas no identifier type character.
Framework Type. The corresponding type in the .NET Framework is the System.Int16 structure.