VB.NET Integer

Examine the Integer type, which is 4 bytes, along with Int32, MinValue and MaxValue.
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.
Because it is everywhere, it is worthwhile to explore this type in more depth. It is a 4-byte numeric type. We can measure its memory usage in an array.ByteKeywords
Integer information: Mapped to: System.Int32 MinValue: -2147483648 MaxValue: 2147483647
An example. We run through some important parts of the Integer type. An Integer is declared with a Dim statement. It can store positive values, such as 1, and negative values, such as -1.

Tip: Expressions in VB.NET do not change the value of variables—unless the variables are assigned to the expression result.

So: Using (number = -1) or (number + 100) with no assignments does not affect the value stored in number.

Note: In this language, an Integer is mapped to the System.Int32 type. It has a range of over four billion values.

Warning: If you try to store any value out of that range, results will be invalid.

VB.NET program that tests Integer type Module Module1 Sub Main() ' Integer Dim. Dim number As Integer = 1 Console.WriteLine(number) ' Assign to negative one. number = -1 Console.WriteLine(number) ' Expressions. Console.WriteLine(number = -1) Console.WriteLine(number + 100) ' Type. Console.WriteLine(number.GetType()) ' Max and min. Console.WriteLine(Integer.MaxValue) Console.WriteLine(Integer.MinValue) ' 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 Module Output 1 -1 True 99 System.Int32 2147483647 -2147483648 4.00032
Notes, program. An Integer is 4 bytes. We allocate 100,000 Integers in an array. Because an Integer is a value type (Structure), its data is all stored within that array.Structure

Info: The program's result, 4.00032, is not exactly four bytes. The extra memory is due to the other data on the evaluation stack and heap.

And: For example, the array reference itself (array) also requires memory—either four or eight bytes.

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

And: The nullable structure can be set to Nothing. We can use GetValueOrDefault to safely get the underlying numeric value (if any).

VB.NET program that uses nullable Integer 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 Module Output GETVALUEORDEFAULT: 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.

For Each, For
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.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls