VB.NET Number Examples

Describe numbers and numeric types, covering Byte, Short, Integer and Long types.

Numbers. Programs are made of numbers. A computer's memory represents numbers in physical bits. In abstraction, we translate these bits and numbers into advanced concepts.Keywords

A foundation. Programs think in terms of numbers. These types are used to build other, more complex, data structures. Numbers are a foundational type.

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.

Maximum: 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.

VB.NET program that uses number 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 Output 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.

Example: We see the MaxValue of UInteger, ULong and UShort. The maximum of ULong is 20 digits.

VB.NET program that uses unsigned types 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 Output 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.IntegerInteger.ParseDoubleByte

Decimal: The Decimal type contains 16 bytes, making it four times the size of an Integer.


Random: 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.

CharChrVal and 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.Boolean

Conversion. Sometimes we have figures in one unit and want to convert them to another unit. Pre-developed methods are ideal for this requirement. Conversion methods can be reused.

Int32: This method converts a value type into an Int32 type. The Integer in VB.NET is an alias for Int32.

Miles, kilometers: We convert units in miles to kilometers, and the opposite. Many numeric conversions can be done in a similar way.

Miles to Kilometers

Math. This class handles mathematical computations. It computes absolute values, maximums, minimums, square roots. The class is reliable and tested.Math.AbsMath.MaxMath.RoundMath.SqrtMod

Tip: Reusing the Math type leads to clearer code. Implementing mathematical functions is a burden.

Select Case. Numbers and Chars can be used in the Select Case statement. This construct can enhance performance. It can also improve code clarity and intention.Select Case

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

Numbers are everywhere. This makes them an excellent topic to study. With related types (Boolean, Char) we are also using numbers. Values may be modified and converted.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to