VB.
NET Data Type
In VB.NET, data type is used to define the type of a variable or function in a program. Furthermore, the conversion of one data type to
another type using the data conversion function.
A Data Type refers to which type of data or value is assigning to a variable or function so that a variable can hold a defined data type value.
For example, when we declare a variable, we have to tell the compiler what type of data or value is allocated to different kinds of variables to
hold different amounts of space in computer memory.
Syntax:
Dim Variable_Name as DataType
VariableName: It defines the name of the variable that you assign to store values.
DataType: It represents the name of the data type that you assign to a variable.
Different Data Types and their allocating spaces in VB.NET
The following table shows the various data types list in the VB.NET programming language.
Data Types Required Space Value Range
Boolean A Boolean type depends on the True or False
implementing platform
Byte 1 byte Byte Range start from 0 to 255 (unsigned)
Char 2 bytes Char Range start from 0 to 65535
(unsigned)
Date 8 bytes Date range can be 0:00:0 (midnight)
January 1, 0001 to 11:5959 PM of December
31, 9999.
Decimal 16 bytes Range from 0 to
+/-79,228,162,514,264,337,593,543,950,335
(+/-7.9…E+28) without any decimal point;
And 0 to
+/-7.92281625142264337593543950335 with
28 position to the right of the decimal
Double 8 bytes -1.79769313486231570E+308 to -4.94-
65645841246544E-324 for negative values;
4.94065645841246544E-324 to
1.79769313486231570E+308, for positive
values
Integer 4 bytes -2,147,483,648 to 2,147,483,647 (signed)
Long 8 bytes -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807 (9.2…E + 18)
(signed)
Object Object size based on the platform such as It can store any type of data defined in a
4 bytes in 32-bit and 8 bytes in 64-bit variable of type Object
platform
SByte 1 byte -128 to 127 (signed)
Short 2 bytes -32,768 to 32,767 (signed)
Single 4 bytes -3.4028235E + 38 to -1.401298E-45 for
negative values;
And for positive value: 1.401298E-45 to
3.4028235E + 38.
String String Datatype depend on the It accepts Unicode character from 0 to
implementing platform approximately 2 billion characters.
UInteger 4 bytes The range start from 0 to 4,294,967,295
(unsigned)
ULong 8 bytes The range of ULong start from 0 to
18,446,744,073,709,551,615 (1.8…E + 19)
(unsigned)
User-Defined (structure) A user-defined data type depends on the Each member of the structure has its
implementing platform own data type and limits independent of
the other members' ranges.
UShort 2 bytes Range from 0 to 65,535 (unsigned)
Let's use the various data types in a VB.NET program.
Data_type.vb
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 Friends..."
Console.WriteLine("Welcome to the JavaTpoint")
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
Output:
Welcome to the JavaTpoint
Byte is: 1
Integer number is: 20
Single data type is: 0.12
Double data type is: 2131.787
Today is: 31-05-2020 00:00:00
Character is: 1
String message is: Hello Friends...
Type Conversion Functions in VB.NET
The following functions are available for conversion.
1. CBool(expression): It is used to convert an expression into a Boolean data type.
2. CByte(expression): It is used to convert an expression to a Byte data type.
3. CChar(expression): It is used to convert an expression to a Char data type.
4. CDate(expression): It is used to convert an expression to a Date data type.
5. CDbl(expression): It is used to convert an expression into a Double data type.
6. CDec(expression): It is used to convert an expression into a Decimal data type.
7. CInt(expression): It is used to convert an expression to an Integer data type.
8. CLng(expression): It is used to convert an expression to a Long data type.
9. CObj(expression): It is used to convert an expression to an Object data type.
10. CSByte(expression): It is used to convert an expression to an SByte data type.
11. CShort(expression): It is used to convert an expression to a Short data type.
12. CSng(expression): It is used to convert an expression into a Single data type.
13. CStr(expression): It is used to convert an expression into a String data type.
14. CUInt(expression): It is used to convert an expression to a UInt data type.
15. CULng(expression): It is used to convert an expression to a ULng data type.
16. CUShort(expression): It is used to convert an expression into a UShort data type.
In the following, program we have performed different conversion.
DB_Conversion.vb
Option Strict On
Module DB_Conversion
Sub Main()
'defining the Data type conversion
Dim dblData As Double
dblData = 5.78
Dim A, B As Char
Dim bool As Boolean = True
Dim x, Z, B_int As Integer
A = "A"
B = "B"
B_int = AscW(B)
Console.WriteLine(" Ascii value of B is {0}", B_int)
x=1
Z = AscW(A)
Z=Z+x
Console.WriteLine("String to integer {0}", Z)
Console.WriteLine("Boolean value is : {0}", CStr(bool))
Dim num, intData As Integer
num = CInt(dblData)
intData = CType(dblData, Integer)
Console.WriteLine(" Explicit conversion of Data type " & Str(intData))
Console.WriteLine(" Value of Double is: {0}", dblData)
Console.WriteLine("Double to Integer: {0}", num)
Console.ReadKey()
End Sub
End Module
Output:
Ascii value of B is 66
String to integer 66
Boolean value is: True
Explicit conversion of Data type 6
Value of Double is: 5.78
Double to Integer: 6
Note: For data type conversion, the VB.NET provides Option Strict On that allows us to convert one data type to another. Some data
types in VB.NET reject the conversion because of "Option Strict On". Remember that while performing conversion, turn off the Option
Strict mode.