UShort data type (Visual Basic)
Holds unsigned 16-bit (2-byte) integers ranging in value from 0 through 65,535.
UShort data type to contain binary data too large for
The default value of
UShort is 0.
You can declare and initialize a
UShort 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
UShort (that is, if it is less than UInt16.MinValue or greater than UInt16.MaxValue, a compilation error occurs.
In the following example, integers equal to 65,034 that are represented as decimal, hexadecimal, and binary literals are assigned to
Dim ushortValue1 As UShort = 65034 Console.WriteLine(ushortValue1) Dim ushortValue2 As UShort = &HFE0A Console.WriteLine(ushortValue2) Dim ushortValue3 As UShort = &B1111_1110_0000_1010 Console.WriteLine(ushortValue3) ' The example displays the following output: ' 65034 ' 65034 ' 65034
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 ushortValue1 As UShort = 65_034 Console.WriteLine(ushortValue1) Dim ushortValue3 As UShort = &B11111110_00001010 Console.WriteLine(ushortValue3) ' The example displays the following output: ' 65034 ' 65034
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 UShort = &H_FF8C
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
us type character to denote the
UShort data type, as the following example shows.
Dim number = &H_5826us
Negative Numbers. Because
UShortis an unsigned type, it cannot represent a negative number. If you use the unary minus (
-) operator on an expression that evaluates to type
UShort, Visual Basic converts the expression to
CLS Compliance. The
UShortdata type is not part of the Common Language Specification (CLS), so CLS-compliant code cannot consume a component that uses it.
UShortdata type widens to
Double. This means you can convert
UShortto any of these types without encountering a System.OverflowException error.
Type Characters. Appending the literal type characters
USto a literal forces it to the
UShorthas no identifier type character.
Framework Type. The corresponding type in the .NET Framework is the System.UInt16 structure.