Short data type (Visual Basic)

Holds signed 16-bit (2-byte) integers that range in value from -32,768 through 32,767.

Remarks

Use the 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.

Literal assignments

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 Short values.

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

Note

You use the prefix &h or &H to denote a hexadecimal literal, the prefix &b or &B to denote a binary literal, and the prefix &o or &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 Select 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

Programming tips

  • Widening. The Short data type widens to Integer, Long, Decimal, Single, or Double. This means you can convert Short to any one of these types without encountering a System.OverflowException error.

  • Type Characters. Appending the literal type character S to a literal forces it to the Short data type. Short has no identifier type character.

  • Framework Type. The corresponding type in the .NET Framework is the System.Int16 structure.

See also