0% found this document useful (0 votes)
3 views

Lecture 3 Labwork Part 1

Uploaded by

Fred Neilly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 3 Labwork Part 1

Uploaded by

Fred Neilly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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:

1. 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 Required Space Value Range


Types

Boolean A Boolean type depends True or False


on the 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.

1|Page
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 It can store any type of data defined in a variable
platform such as 4 bytes of type Object
in 32-bit and 8 bytes in
64-bit 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 It accepts Unicode character from 0 to


on the implementing approximately 2 billion characters.
platform

UInteger 4 bytes The range start from 0 to 4,294,967,295


(unsigned)

2|Page
ULong 8 bytes The range of ULong start from 0 to
18,446,744,073,709,551,615 (1.8…E + 19)
(unsigned)

User- A user-defined data type Each member of the structure has its own data
Defined depends on the type and limits independent of the other
(structure) implementing platform 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

1. Module Data_type
2. Sub Main()
3. ' defining the Data Type to the variables
4. Dim b As Byte = 1
5. Dim num As Integer = 5
6. Dim si As Single
7. Dim db As Double
8. Dim get_date As Date
9. Dim c As Char
10. Dim str As String
11.
12. b=1
13. num = 20
14. si = 0.12
15. db = 2131.787
16. get_date = Today
17. c = "A"
18. str = "Hello Friends..."
19.
20. Console.WriteLine("Welcome to the VB.NET Data Types")
21. Console.WriteLine("Byte is: {0}", b)

3|Page
22. Console.WriteLine("Integer number is: {0}", num)
23. Console.WriteLine("Single data type is: {0}", si)
24. Console.WriteLine("Double data type is: {0}", db)
25. Console.WriteLine("Today is: {0}", get_date)
26. Console.WriteLine("Character is: {0}", b)
27. Console.WriteLine("String message is: {0}", str)
28. Console.ReadKey()
29. End Sub
30. End Module

Output:

Welcome to the VB.NET Data Types


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.

4|Page
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

1. Option Strict On
2. Module DB_Conversion
3. Sub Main()
4. 'defining the Data type conversion
5. Dim dblData As Double
6. dblData = 5.78
7. Dim A, B As Char
8. Dim bool As Boolean = True
9. Dim x, Z, B_int As Integer
10. A = "A"
11. B = "B"
12. B_int = AscW(B)
13.
14. Console.WriteLine(" Ascii value of B is {0}", B_int)
15.
16. x=1
17. Z = AscW(A)
18. Z=Z+x
19. Console.WriteLine("String to integer {0}", Z)
20. Console.WriteLine("Boolean value is : {0}", CStr(bool))
21. Dim num, intData As Integer
22.
23. num = CInt(dblData)

5|Page
24. intData = CType(dblData, Integer)
25. Console.WriteLine(" Explicit conversion of Data type " & Str(intData))
26. Console.WriteLine(" Value of Double is: {0}", dblData)
27. Console.WriteLine("Double to Integer: {0}", num)
28. Console.ReadKey()
29. End Sub
30. 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.

6|Page

You might also like