Muokkaa

Jaa


SByte data type (Visual Basic)

Holds signed 8-bit (1-byte) integers that range in value from -128 through 127.

Remarks

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

Literal assignments

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

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 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>

For more information see Select the Visual Basic language version.

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 0x9A and 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 /removeintchecks compiler switch.

  • 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 Short value 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 Short value.

    Dim sByteValue1 As SByte = &HFF_9As
    Dim sByteValue2 As SByte = &B1111_1111_1001_1010s
    Console.WriteLine(sByteValue1)
    Console.WriteLine(sByteValue2)
    

Programming tips

  • CLS Compliance. The SByte data type is not part of the Common Language Specification (CLS), so CLS-compliant code cannot consume a component that uses it.

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

  • Type Characters. SByte has no literal type character or identifier type character.

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

See also