description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: Integer data type (Visual Basic) |
Integer Data Type |
01/31/2018 |
|
|
a8f233b4-4be3-455c-861b-05af2fbb6c60 |
Holds signed 32-bit (4-byte) integers that range in value from -2,147,483,648 through 2,147,483,647.
The Integer
data type provides optimal performance on a 32-bit processor. The other integral types are slower to load and store from and to memory.
The default value of Integer
is 0.
You can declare and initialize an Integer
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 Integer
(that is, if it is less than xref:System.Int32.MinValue?displayProperty=nameWithType or greater than xref:System.Int32.MaxValue?displayProperty=nameWithType, a compilation error occurs.
In the following example, integers equal to 90,946 that are represented as decimal, hexadecimal, and binary literals are assigned to Integer
values.
[!code-vbinteger]
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-vbinteger]
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 Integer = &H_C305_F860
[!INCLUDE supporting-underscores]
Numeric literals can also include the I
type character to denote the Integer
data type, as the following example shows.
Dim number = &H_035826I
-
Interop Considerations. If you are interfacing with components not written for the .NET Framework, such as Automation or COM objects, remember that
Integer
has a different data width (16 bits) in other environments. If you are passing a 16-bit argument to such a component, declare it asShort
instead ofInteger
in your new Visual Basic code. -
Widening. The
Integer
data type widens toLong
,Decimal
,Single
, orDouble
. This means you can convertInteger
to any one of these types without encountering a xref:System.OverflowException?displayProperty=nameWithType error. -
Type Characters. Appending the literal type character
I
to a literal forces it to theInteger
data type. Appending the identifier type character%
to any identifier forces it toInteger
. -
Framework Type. The corresponding type in the .NET Framework is the xref:System.Int32?displayProperty=nameWithType structure.
If you try to set a variable of an integral type to a number outside the range for that type, an error occurs. If you try to set it to a fraction, the number is rounded up or down to the nearest integer value. If the number is equally close to two integer values, the value is rounded to the nearest even integer. This behavior minimizes rounding errors that result from consistently rounding a midpoint value in a single direction. The following code shows examples of rounding.
' The valid range of an Integer variable is -2147483648 through +2147483647.
Dim k As Integer
' The following statement causes an error because the value is too large.
k = 2147483648
' The following statement sets k to 6.
k = 5.9
' The following statement sets k to 4
k = 4.5
' The following statement sets k to 6
' Note, Visual Basic uses banker’s rounding (toward nearest even number)
k = 5.5
- xref:System.Int32?displayProperty=nameWithType
- Data Types
- Long Data Type
- Short Data Type
- Type Conversion Functions
- Conversion Summary
- Efficient Use of Data Types