VB.NET Data types

This article will cover the use of the various data types in VB.NET

Every language has it’s own set of unique Data types. Each data type has a different size and is defined for a certain type of data. You’ll see data types which feel like they might be identical, like Integer and Long, which are both for numbers. However the difference between them is in their size. This is done to increase the efficiency and decrease memory consumption. Each time a variable is declared, it will be allotted memory equal to it’s maximum size, regardless of whether it was using all of it or not.


VB.NET Data types

Below is the compilation of all the datatypes in VB.NET along with a brief description as to their size and what kind of data they store.

NameSizeDescription
BooleanDepends on the platformStores either a True or False Value
Byte1 ByteStores values from 0 to 255
Char2 BytesStores values from 0 to 65535
Date8 BytesStores Dates from January 1, 0001 to December 31, 9999
Single4 Bytes-3.402823E+38 to -1.401298E-45 for negative values;
1.401298E-45 to 3.402823E+38 for positive values
Double8 Bytes-1.79769313486231E+308 to 4.94065645841247E-324 for negative
values;
4.94065645841247E-324 to 1.79769313486231E+308 for positive
values
Decimal16 Bytes +/-79,228,162,514,264,337,593,543,950,335 with no decimal
point; +/-7.9228162514264337593543950335 with 28 places to the
right of the decimal; smallest non-zero number is
+/-0.0000000000000000000000000001
Short2 BytesStores numerical values from -32,768 to 32,767
Integer4 BytesStores numerical values from -2,147,483,648 to 2,147,483,647
Long8 BytesStores numerical values from
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
StringDepends on the platform Stores characters from 0 to 2 billion characters

Examples

The below examples show many of the above data types and demonstrates the different scenarios where one may be used. Notice the different values for Short, Integer and Long are used. Due to the massive range required to host exponential values, using long would be advisable wherever exponential values are involved. Same concept applies to other data types.

Module Module1
    Sub Main()
        Dim a As Short
        a = 50 * 10
        Console.WriteLine(a)

        Dim b As Integer
        b = 300 * 400
        Console.WriteLine(b)

        Dim c As Long
        c = 40 ^ 6
        Console.WriteLine(c)

        Dim d As Byte
        d = 200
        Console.WriteLine(d)

        Dim e As Boolean
        e = True
        Console.WriteLine(e)

        Dim f As String
        f = "Hello world. This is CodersLegacy"
        Console.WriteLine(f)

        Dim g As Single
        g = 324.6765
        Console.WriteLine(g)

        Dim h As Decimal
        h = 324.676545352787
        Console.WriteLine(h)

    End Sub
End Module

This marks the end of the VB.NET Data types Article. Any suggestions are contributions for our site, CodersLegacy are more than welcome. Any questions can be asked below in the comments section.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments