VB.net Data Type with Example Tutorial

VB.net Data Type

VB.net Data Type are a complex mechanism for defining variables and operations of various types. The type of a variable dictates how much storage space it takes up and how the stored bit pattern is interpreted.

The Data Type in VB.NET is used to specify the type of a variable or function in a program. Furthermore, the data conversion function is used to convert one data type to another.

A Data Sort specifies the type of data or value that should be assigned to a variable or function in order for it to carry a specific data type value. When we declare a variable, for example, we must inform the compiler what sort of data or value is assigned to different types of variables in order for them to hold different amounts of memory in the computer.

MySQL VB.NET Tutorial: How to Conne...
MySQL VB.NET Tutorial: How to Connect VB.Net to MySQL Database (2019) Best Practices

In order for you to test your VB.net Code provided in this lesson, you must test the code in your code editor. But if you wish to run this code online, we also have an Online Compiler in VB.net for you to test your VB.net Code for free.

Available Data Types in VB.net

VB.net provides a wide range of data types. The following table shows all the data types available in vb.net

VB.net Data TypeStorage AllocationValue Range
BooleanDepends on implementing platformTrue or False
Byte1 byte0 through 255 (unsigned)
Char2 bytes0 through 65535 (unsigned)
Date8 bytes0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM on December 31, 9999
Decimal16 bytes0 through +/-79,228,162,514,264,337,593,543,950,335 (+/-7.9…E+28) with no decimal point; 0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal
Double8 bytes-1.79769313486231570E+308 through -4.94065645841246544E-324, for negative values4.94065645841246544E-324 through 1.79769313486231570E+308, for positive values
Integer4 bytes-2,147,483,648 through 2,147,483,647 (signed)
Long8 bytes-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807(signed)
Object4 bytes on 32-bit platform8 bytes on 64-bit platformAny type can be stored in a variable of type Object
SByte1 byte-128 through 127 (signed)
Short2 bytes-32,768 through 32,767 (signed)
Single4 bytes-3.4028235E+38 through -1.401298E-45 for negative values;1.401298E-45 through 3.4028235E+38 for positive values
StringDepends on implementing platform0 to approximately 2 billion Unicode characters
UInteger4 bytes0 through 4,294,967,295 (unsigned)
ULong8 bytes0 through 18,446,744,073,709,551,615 (unsigned)
User-DefinedDepends on implementing platformEach member of the structure has a range determined by its data type and independent of the ranges of the other members
UShort2 bytes0 through 65,535 (unsigned)
Available Data Types in VB.net

VB.net Types Example

The following example demonstrates use of some of the VB.net types.

Module Data_type  
    Sub Main()  
' defining the Data Type to the variables  
        Dim b As Byte = 1  
        Dim num As Integer = 5  
        Dim si As Single  
        Dim db As Double  
        Dim get_date As Date  
        Dim c As Char  
        Dim str As String  
  
        b = 1  
        num = 20  
        si = 0.12  
        db = 2131.787  
        get_date = Today  
        c = "A"  
        str = "Hello IT SOURCECODERS..."  
  
        Console.WriteLine("Welcome to the IT SOURCECODE")  
        Console.WriteLine("Byte is: {0}", b)  
        Console.WriteLine("Integer number is: {0}", num)  
        Console.WriteLine("Single data type is: {0}", si)  
        Console.WriteLine("Double data type is: {0}", db)  
        Console.WriteLine("Today is: {0}", get_date)  
        Console.WriteLine("Character is: {0}", b)  
        Console.WriteLine("String message is: {0}", str)  
        Console.ReadKey()  
    End Sub  
End Module

When the above code is compiled and executed, it produces the following result.

Welcome to the IT SOURCECODE
Byte is: 1
Integer number is: 20
Single data type is: 0.12
Double data type is: 2131.787
Today is: 6/20/2022 12:00:00 AM
Character is: 1
String message is: Hello IT SOURCECODERS…

Test your Code Here!

The Type Conversion Functions in VB.net

VB.net provides the following in-line type conversion functions.

Sr.No.Functions & Description
1.CBool(expression)Converts the expression to Boolean data type.
2.CByte(expression)Converts the expression to Byte data type.
3.CChar(expression)Converts the expression to Char data type.
4.CDate(expression)Converts the expression to Date data type
5.CDbl(expression)Converts the expression to Double data type.
6.CDec(expression)Converts the expression to Decimal data type.
7.CInt(expression)Converts the expression to Integer data type.
8.CLng(expression)Converts the expression to Long data type.
9.CObj(expression)Converts the expression to Object type.
10.CSByte(expression)Converts the expression to SByte data type.
11.CShort(expression)Converts the expression to Short data type.
12.CSng(expression)Converts the expression to Single data type.
13.CStr(expression)Converts the expression to String data type.
14.CUInt(expression)Converts the expression to UInt data type.
15.CULng(expression)Converts the expression to ULng data type.
16.CUShort(expression)Converts the expression to UShort data type.
Type Conversion Functions in VB.net

VB.net Functions Example

The following example demonstrates some of these functions.

Module DataTypes
   Sub Main()
      Dim n As Integer
      Dim da As Date
      Dim bl As Boolean = True
      n = 1234567
      da = Today
      
      Console.WriteLine(bl)
      Console.WriteLine(CSByte(bl))
      Console.WriteLine(CStr(bl))
      Console.WriteLine(CStr(da))
      Console.WriteLine(CChar(CChar(CStr(n))))
      Console.WriteLine(CChar(CStr(da)))
      Console.ReadKey()
   End Sub
End Module

When the above code is compiled and executed, it produces the following result.

True
-1
True
6/20/2022
1
6

Test your Code Here!

Summary

Each variable must belong to a data type. The data type determines the amount of memory space allocated to the variable. We can convert a variable from one data type to another.

Initializing variables means assigning values to the variables. We create a console application to help us get input from the users via the console using the ReadLine function.


Leave a Comment