Integer. This is a core type in VB.NET programs. It is the default numeric type. Usually loops use the Integer type for their indexes.
Integer details. Because it is everywhere, it is worthwhile to explore this .NET type in more depth. This is a 4-byte numeric type. We can measure its memory usage in an array.
Module Module1
Sub Main()
' Part 1: specify an Integer Dim.
Dim number As Integer = 1
Console.WriteLine(number)
number = -1
Console.WriteLine(number)
' Part 2: use expressions.
Console.WriteLine(number = -1)
Console.WriteLine(number + 100)
' Part 3: get type and maximum and minimum values.
Console.WriteLine(number.GetType())
Console.WriteLine(Integer.MaxValue)
Console.WriteLine(Integer.MinValue)
' Part 4: compute memory per Integer.
Dim bytes1 = GC.GetTotalMemory(False)
Dim array(100000) As Integer
array(0) = 1
Dim bytes2 = GC.GetTotalMemory(False)
Console.WriteLine((bytes2 - bytes1) / 100000)
End Sub
End Module1
-1
True
99
System.Int32
2147483647
-2147483648
4.00032
Some important values. It is worthwhile to emphasize the minimum and maximum values for an integer. Often our programs will access Integer.MinValue and MaxValue for bounds.
Mapped to: System.Int32
MinValue: -2147483648
MaxValue: 2147483647
Nullable Integer. An Integer is a value type, so it cannot be set to Nothing. But we can wrap an Integer in a Nullable Structure, with special syntax in VB.NET.
And The nullable structure can be set to Nothing. We can use GetValueOrDefault to safely get the underlying numeric value (if any).
Module Module1
Sub Main()
' A nullable Integer can be a numeric value, or Nothing.
Dim test As Integer? = 100
Console.WriteLine("GETVALUEORDEFAULT: {0}", test.GetValueOrDefault())
test = Nothing
Console.WriteLine("GETVALUEORDEFAULT: {0}", test.GetValueOrDefault())
End Sub
End ModuleGETVALUEORDEFAULT: 100
GETVALUEORDEFAULT: 0
Loops. As a fundamental type, the Integer is used in For loops as an index. In languages, the integer type (int) is usually optimized for performance.
Thus In most programs, the int as a local variable is the fastest. It is worth keeping int for loop indexes.
A summary. Integer is a fundamental type in VB.NET programs. It is mapped to System.Int32, which makes it precisely equivalent, in the C# language, to int. It requires four bytes of memory.
Integer has well-known lower and upper values. It has low-level optimizations for performance. This core type is worth researching. It is used throughout software projects.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 20, 2024 (edit link).