Number ExamplesUnderstand numbers and numeric types, covering Byte, Short, Integer and Long types.
This page was last reviewed on Mar 21, 2022.
Numbers. In VB.NET, we use various number types. Numbers are a foundational type throughout programs. And most commonly, we use Integer.
Numbers are structures. They are copied on assignment. Their data is stored (represented) within the variables themselves. And numbers do not use heap allocation like objects.
Integer. The Integer type is used throughout every program. Number types vary in size. Some use more memory—these represent larger, smaller or more detailed numbers.
Detail Each numeric type—Byte, Short, Integer, Long—has a different maximum value. This is the highest value the type can represent.
Here In this program, we display the maximum values for Byte, Short, Integer and Long types.
Module Module1 Sub Main() Dim a As Byte = Byte.MaxValue Dim b As Short = Short.MaxValue Dim c As Integer = Integer.MaxValue Dim d As Long = Long.MaxValue Console.WriteLine(a) Console.WriteLine(b) Console.WriteLine(c) Console.WriteLine(d) End Sub End Module
255 32767 2147483647 9223372036854775807
Unsigned. There are unsigned types available. These omit the sign bit, so they can store a larger maximum value than the equivalent signed types.
Detail We see the MaxValue of UInteger, ULong and UShort. The maximum of ULong is 20 digits.
Module Module1 Sub Main() ' Demonstrate some unsigned types. Dim value1 As UInteger = UInteger.MaxValue Dim value2 As ULong = ULong.MaxValue Dim value3 As UShort = UShort.MaxValue Console.WriteLine(value1) Console.WriteLine(value2) Console.WriteLine(value3) End Sub End Module
4294967295 18446744073709551615 65535
Types. An Integer is four bytes. This is often the fastest type for local variables in methods. It does not handle decimal values. A double is 8 bytes. A byte is just one.
Detail The Decimal type contains 16 bytes, making it four times the size of an Integer.
Detail The Random class generates unpredictable integers. We optionally specify a minimum and maximum.
A character is a value, similar to an Integer or UShort. The Char type represents a character. Certain functions such as Chr, Val and Asc also handle character values.
Tip The String Class extensively uses Char values. Each String internally contains Chars.
Val, Asc
A Boolean value represents true or false, yes or no. It is represented by one byte in this language. It can be used in an If-statement or While-loop.
Math. This class handles mathematical computations. It computes absolute values, maximums, minimums, square roots. The class is reliable and tested.
Math.Max, Math.Min
Tip Reusing the Math type leads to clearer code. Implementing mathematical functions is a burden.
A summary. Numbers like Integer are everywhere in VB.NET. With related types (Boolean, Char) we are also using numbers. Values may be modified and converted.
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 Mar 21, 2022 (image).
© 2007-2023 Sam Allen.