C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Part 1: An Integer is declared with a Dim statement. It can store positive values, such as 1, and negative values, such as -1.
Part 2: Expressions in VB.NET do not change the value of variables—unless the variables are assigned to the expression result.
Part 3: In this language, an Integer is mapped to the System.Int32 type. It has a range of over 4 billion values.
Part 4: We allocate an Integer array. We determine that each Integer occupies 4 bytes (all data is stored within the array).
StructureVB.NET program that tests Integer type
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 Module
Output
1
-1
True
99
System.Int32
2147483647
-2147483648
4.00032
Integer information:
Mapped to: System.Int32
MinValue: -2147483648
MaxValue: 2147483647
And: The nullable structure can be set to Nothing. We can use GetValueOrDefault to safely get the underlying numeric value (if any).
NullableVB.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
Thus: In most programs, the int as a local variable is the fastest. It is worth keeping int for loop indexes.
For Each, For