There are three types of variables for storing integers, and they differ only in the range of numbers each can represent. As you understand, the more bytes a type takes, the larger values it can hold. The type of Integer variable you’ll use depends on the task at hand. You should choose the type that can represent the largest values you anticipate will come up in your calculations. You can go for the Long type, to be safe, but Long variables are four times as large as Short variables, and it takes the computer longer to process them.
The statements in Listing 2.1 will help you understand when to use the various Integer data types. Each numeric data type exposes the MinValue and MaxValue properties, which return the minimum and maximum values, respectively, that can be represented by the corresponding data type. Values of the Short (Int16) type can be stored in Integer (Int32) and Long (Int64) variables, but the reverse is not true. If you attempt to store a Long value to an Integer variable, an error will be generated and the compiler will underline the offending line with a wiggly line. I have included comments after each statement to explain the errors produced by some of the statements.
Listing 2.1: Experimentingwith the Ranges of Numeric Variables
Code language: VB.NET (vbnet)
Dim shortInt As Int16 Dim Int As Int32 Dim longInt As Int64 Debug.WriteLine(Int16.MinValue) Debug.WriteLine(Int16.MaxValue) Debug.WriteLine(Int32.MinValue) Debug.WriteLine(Int32.MaxValue) Debug.WriteLine(Int64.MinValue) Debug.WriteLine(Int64.MaxValue) shortInt = Int16.MaxValue + 1 ' ERROR, exceeds the maximum value of the Short data type Int = Int16.MaxValue + 1 ' OK, is within the range of the Integer data type Int = Int32.MaxValue + 1 ' ERROR, exceeds the maximum value of the Integer data type Int = Int32.MinValue - 1 ' ERROR, exceeds the minimum value of the Integer data type longInt = Int32.MaxValue + 1 ' OK, is within the range of the Long data type longInt = Int64.MaxValue + 1 ' ERROR, exceeds the range of all Integer data types
The six WriteLine statements will print the minimum and maximum values you can represent with the various Integer data types. The following statement attempts to assign to a Short integer variable a value that exceeds the largest possible value you can represent with the Short data type, and it will generate an error. The editor will underline the incorrect statement, and if you hover the pointer over the statement, you’ll see the error description: Constant expression not representable in type Short. If you attempt to store the same value to an Integer variable, there will be no problem because this value is well within the range of the Integer data type.
The next two statements attempt to store to an Integer variable two values that are also outside of the range that an integer can represent. The first value exceeds the range of positive values, and the second exceeds the range of negative values. If you attempt to store these values to a Long variable, there will be no problem. If you exceed the range of values that can be represented by the Long data type, you’re out of luck. This value can’t be represented as an integer, and you must store it in one of the variable types discussed in the next sections.