다음을 통해 공유


Integer Data Type (Visual Basic)

Holds signed 32-bit (4-byte) integers that range in value from -2,147,483,648 through 2,147,483,647.

Remarks

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.

Programming Tips

  • Interop Considerations. If you are interfacing with components not written for the .NET Framework, for example 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 as Short instead of Integer in your new Visual Basic code.

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

  • Type Characters. Appending the literal type character I to a literal forces it to the Integer data type. Appending the identifier type character % to any identifier forces it to Integer.

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

Range

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

See Also

Reference

Data Type Summary (Visual Basic)

System.Int32

Long Data Type (Visual Basic)

Short Data Type (Visual Basic)

Type Conversion Functions (Visual Basic)

Conversion Summary (Visual Basic)

Concepts

Efficient Use of Data Types (Visual Basic)