SByte data type (Visual Basic)
Holds signed 8-bit (1-byte) integers that range in value from -128 through 127.
SByte data type to contain integer values that do not require the full data width of
Integer or even the half data width of
Short. In some cases, the common language runtime might be able to pack your
SByte variables closely together and save memory consumption.
The default value of
SByte is 0.
You can declare and initialize an
SByte variable by assigning it a decimal literal, a hexadecimal literal, an octal literal, or (starting with Visual Basic 2017) a binary literal.
In the following example, integers equal to -102 that are represented as decimal, hexadecimal, and binary literals are assigned to
SByte values. This example requires that you compile with the
/removeintchecks compiler switch.
Dim sbyteValue1 As SByte = -102 Console.WriteLine(sbyteValue1) Dim sbyteValue4 As SByte = &H9A Console.WriteLine(sbyteValue4) Dim sbyteValue5 As SByte = &B1001_1010 Console.WriteLine(sbyteValue5) ' The example displays the following output: ' -102 ' -102 ' -102
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 sbyteValue3 As SByte = &B1001_1010 Console.WriteLine(sbyteValue3) ' The example displays the following output: ' -102
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 SByte = &H_F9
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>
If the integer literal is outside the range of
SByte (that is, if it is less than SByte.MinValue or greater than SByte.MaxValue, a compilation error occurs. When an integer literal has no suffix, an Integer is inferred. If the integer literal is outside the range of the
Integer type, a Long is inferred. This means that, in the previous examples, the numeric literals
0b10011010 are interpreted as 32-bit signed integers with a value of 156, which exceeds SByte.MaxValue. To successfully compile code like this that assigns a non-decimal integer to an
SByte, you can do either of the following:
Disable integer bounds checks by compiling with the
Use a type character to explicitly define the literal value that you want to assign to the
SByte. The following example assigns a negative literal
Shortvalue to an
SByte. Note that, for negative numbers, the high-order bit of the high-order word of the numeric literal must be set. In the case of our example, this is bit 15 of the literal
Dim sByteValue1 As SByte = &HFF_9As Dim sByteValue2 As SByte = &B1111_1111_1001_1010s Console.WriteLine(sByteValue1) Console.WriteLine(sByteValue2)
CLS Compliance. The
SBytedata type is not part of the Common Language Specification (CLS), so CLS-compliant code cannot consume a component that uses it.
SBytedata type widens to
Double. This means you can convert
SByteto any of these types without encountering a System.OverflowException error.
SBytehas no literal type character or identifier type character.
Framework Type. The corresponding type in the .NET Framework is the System.SByte structure.