Skip to content

Latest commit

 

History

History
76 lines (54 loc) · 3.74 KB

short-data-type.md

File metadata and controls

76 lines (54 loc) · 3.74 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
Learn more about: Short data type (Visual Basic)
Short Data Type
01/31/2018
vb.Short
numbers [Visual Basic], whole
whole numbers
integral data types [Visual Basic]
integer numbers
numbers [Visual Basic], integer
integers [Visual Basic], data types
integers [Visual Basic], types
data types [Visual Basic], integral
S literal type character [Visual Basic]
Short data type
literal type characters [Visual Basic], S
65fcbcf3-a841-400e-885e-301497729a8b

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 xref:System.Int16.MinValue?displayProperty=nameWithType or greater than xref:System.Int16.MaxValue?displayProperty=nameWithType, 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.

[!code-vbShort]

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.

[!code-vbShort]

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

[!INCLUDE supporting-underscores]

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 xref:System.OverflowException?displayProperty=nameWithType 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 xref:System.Int16?displayProperty=nameWithType structure.

See also