0% found this document useful (0 votes)
50 views16 pages

Cpprog1 - Chapter 3 PDF

The document discusses data types, variables, and constants in VB.NET programming. It defines different data types like integer, string, boolean, and their ranges. It shows how to declare variables with data types and assign values. The document also covers type conversion functions to change one data type to another.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views16 pages

Cpprog1 - Chapter 3 PDF

The document discusses data types, variables, and constants in VB.NET programming. It defines different data types like integer, string, boolean, and their ranges. It shows how to declare variables with data types and assign values. The document also covers type conversion functions to change one data type to another.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MODULE INTRO TO VB.

NET PROGRAMMING– CPPROG1

CHAPTER 3: VB.NET DATA TYPES, VARIABLES &


CONSTANT

Objectives:
a.) Discuss the primitive data types used in any programming
languages.
b.) Differentiate the variable and constant.
b.) Discover the syntax for declaring a variable and a constant.

Lesson 1: 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 on True or False


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)

Page | 1
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

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


the implementing platform approximately 2 billion characters.

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


(unsigned)

Page | 2
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

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 implementing type and limits independent of the other
(structure) 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 JavaTpoint")
21. Console.WriteLine("Byte is: {0}", b)
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()

Page | 3
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

29. End Sub


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

Page | 4
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

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)
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

Page | 5
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

For more knowledge about primitive data types, please check the link provided;
https://www.youtube.com/watch?v=IneVm5aY2nM&list=PLC601DEA22187BBF1&in
dex=5

Lesson 2: Variable
What is a Variable?
A variable is a simple name used to store the value of a specific data type in computer
memory. In VB.NET, each variable has a particular data type that determines the size, range, and
fixed space in computer memory. With the help of variable, we can perform several operations
and manipulate data values in any programming language.
VB.NET Variables Declaration
The declaration of a variable is simple that requires a variable name and data type followed by a
Dim. A Dim is used in Class, Module, structure, Sub, procedure.
Syntax:

1. Dim [Variable_Name] As [Defined Data Type]

Name Descriptions

Dim It is used to declare and allocate the space for one or more variables in
memory.

Variable_Name It defines the name of the variable to store the values.

As It is a keyword that allows you to define the data type in the declaration
statement.

Data Type It defines a data type that allows variables to store data types such as Char,
String, Integer, Decimal, Long, etc.

Value Assign a value to the variable.


There are some valid declarations of variables along with their data type definition, as shown
below:
1. Dim Roll_no As Integer
2. Dim Emp_name As String

Page | 6
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

3. Dim Salary As Double


4. Dim Emp_id, Stud_id As Integer
5. Dim result_status As Boolean

Further, if we want to declare more than one variable in the same line, we must separate each
variable with a comma.

Syntax
1. Dim Variable_name1 As DataType1, variable_name2 As DataType2, Variable_name3 As
DataType3
Note: The statements given below is also used to declare the variable with their data type:
1. Static name As String
2. Public bill As Decimal = 0

VB.NET Variable Initialization


After the declaration of a variable, we must assign a value to the variable. The following syntax
describes the initialization of a variable:
Syntax:
1. Variable_name = value
For example:
1. Dim Roll_no As Integer 'declaration of Roll_no
2. Roll_no = 101 'initialization of Roll_no
3.
4. Initialize the Emp_name
5. Dim Emp_name As String
6. Emp_name = "David" 'Here Emp_name variable assigned a value of David
7.
8. Initialize a Boolean variable
9. Dim status As Boolean 'Boolean value can be True or False.
10. status = True 'Initialize status value to True
We can also initialize a variable at the time of declaration:
1. Dim Roll_no As Integer = 101
2. Dim Emp_name As String = " Stephen Robert "

Let's create a program to use different types of variable declaration and initialization in VB.NET.

Page | 7
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

Variable1.vb
1. Imports System
2. Module Variable1
3. Sub Main()
4. 'declaration of intData as Integer
5. Dim intData As Integer
6. 'declaration of charData as Char
7. Dim CharData As Char
8. 'declaration of strData as String
9. Dim strData As String
10. 'declaration of dblData as Double
11. Dim dblData As Double
12. 'declaration of single_data as Single
13. Dim single_data As Single
14. 'Initialization of intData
15. intData = 10
16. 'Initialization of CharData
17. CharData = "A"
18. 'Initialization of strData
19. strData = " VB.NET is a Programming Language."
20. dblData = 4567.676
21. 'Initialization of dblData
22. 'Initialization of single_data
23. single_data = 23.08
24.
25. Console.WriteLine(" Value of intData is: {0}", intData)
26. Console.WriteLine(" Value of CharData is: {0}", CharData)
27. Console.WriteLine(" Value of strData is: {0}", strData)
28. Console.WriteLine(" Value of dblData is: {0}", dblData)
29. Console.WriteLine(" Value of single_data is: {0}", single_data)
30.
31. Console.WriteLine("press any key to exit...")
32. Console.ReadKey()
33. End Sub
34.
35. End Module
Output:
Value of intData is: 10
Value of CharData is: A
Value of strData is: VB.NET is a Programming Language.
Page | 8
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

Value of dblData is: 4567.676


Value of single_data is: 23.08
press any key to exit...
Getting Values from the User in VB.NET
In VB.NET, the Console class provides the Readline() function in the System namespace. It is used
to take input from the user and assign a value to a variable. For example:
1. Dim name As String
2. name = Console.ReadLine()

3. Or name = Console.ReadLine
Let's create a program that takes input from the user.
User_Data.vb
1. Imports System
2. Module User_Data
3. Sub Main()
4. Dim num As Integer
5. Dim age As Double
6. Dim name As String
7. Console.WriteLine("Enter your favourite number")
8. ' Console.ReadLine or Console.ReadLine() takes value from the user
9. num = Console.ReadLine
10. Console.WriteLine(" Enter Your Good name")
11. 'Read string data from the user
12. name = Console.ReadLine
13. Console.WriteLine(" Enter your Age")
14. age = Console.ReadLine
15. Console.WriteLine(" You have entered {0}", num)
16. Console.WriteLine(" You have entered {0}", name)
17. Console.WriteLine(" You have entered {0}", age)
18. Console.ReadKey()
19.
20. End Sub
21. End Module
Output:

Enter your favourite number


7
Enter Your Good name

Page | 9
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

Alexander
Enter your Age
27.5
You have entered 7
You have entered Alexander
You have entered 27.5

Note: Console.Read and Console.ReadKey() function is used to read a single character from the
user.
Lvalues and Rvalues in VB.NET
There are two ways to express the expression value:

Lvalue: It is an lvalue expression that refers to a memory location for storing the address of a
variable. An lvalue is a variable that can appear to the left or right of the assignment operator to
hold values. Furthermore, in comparison to or swapping the variables' values, we can also define
the variable on both sides (left or right-side) of the assignment operator.
Example:
1. Dim num As Integer
2. Num = 5
3. Or
4. Dim num As Integer = 5
But when we write the following statement, it generates a compile-time error because it is not a
valid statement.
1. Dim x As Integer
2. 10 = x
Rvalue: It is an rvalue expression that is used to store a value in some address of memory. An
rvalue can appear only on the right- hand side because it is a value of the variable that defines
on the right-hand side.
1. Dim name As String
2. Name = "Peter" // rvalue define at right side of the assignment operator.

For more knowledge about variables, please check the link provided;
https://www.youtube.com/watch?v=r8GHJyFOP18&list=PLC601DEA22187BBF1&ind
ex=8

Page | 10
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

Lesson 3: Constant
VB.NET Constants
As the name suggests, the name constant refers to a fixed value that cannot be changed
during the execution of a program. It is also known as literals. These constants can be of any data
type, such as Integer, Double, String, Decimal, Single, character, enum, etc.
Declaration of Constants

In VB.NET, const is a keyword that is used to declare a variable as constant. The Const statement
can be used with module, structure, procedure, form, and class.

Syntax:
1. Const constname As datatype = value

Item Descriptions
Name

Const It is a Const keyword to declare a variable as constant.

Constname It defines the name of the constant variable to store the values.

As It is a keyword that allows you to define the data type in the declaration
statement.

Data Type It defines a data type that allows variables to store data types such as Char,
String, Integer, Decimal, Long, etc.

Value Assign a value to the variable as constant.


Further, if we want to declare more than one variable in the same line, we must separate each
variable with a comma, as shown below. The Syntax for defining the multiple variables as
constant is:
1. Dim Variable_name1 As DataType1, variable_name2 As DataType2, Variable_name3 As
DataType3
Note: The statements given below are also used to declare the variable with their data type:
1. Const num As Integer = 10
2. Static name As String
3. Public Const name As String = "JavaTpoint"

Page | 11
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

4. Private Const PI As Double = 3.14


Example of Const keyword
Const1.vb

1. Module Const1
2. Sub main()
3. 'declaration and initialization of Constant variable using Const keywords
4. Const intData As Integer = 20
5. Const name As String = "JavaTpoint"
6. Const topic As String = "VB.NET"
7. Const PI = 3.14
8. Dim radius, area As Integer
9.
10. Console.WriteLine(" Constant integer is {0}", intData)
11. Console.WriteLine(" You have entered {0}", name)
12. Console.WriteLine(" Your Topic is {0}", topic)
13. Console.WriteLine("Enter the Radius")
14. radius = Console.ReadLine()
15. area = PI * radius * radius
16. Console.WriteLine(" Area of Circle is {0}", area)
17. Console.ReadKey()
18.
19. End Sub
20. End Module
Output:
Constant integer is 20
You have entered JavaTpoint
Your Topic is VB.NET
Enter the Radius
7
Area of Circle is 154
Scope of Variable in VB.NET

The scope of a variable determines the accessible range of a defined variable at the time of
declaration in any block, module, and class. You can access it if the variable is in a particular
region or scope in the same block. And if the variable goes beyond the region, its scope expires.
The following are the methods to represent the scope of a variable in VB.NET.

1. Procedure Scope
2. Module Scope
Page | 12
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

3. Public Scope
Procedure (local) scope
A local variable is a type of variable defined within a procedure scope, block, or function. It is
available with a code inside the procedure, and it can be declared using the Dim or
static statement. These variables are not accessible from outside of the local method. However,
the local variable can be easily accessed by the nested programming function in the same
method.
1. Dim X As Integer
Local variables exist until the procedure in which they are declared is executed. Once a procedure
is executed, the values of its local variables will be lost, and the resources used by these variables
will be released. And when the block is executed again, all the local variables are rearranged.
Let's create a program that displays the local scope of a variable within a function.
Local_Scope.vb

1. Imports System
2. Module Local_scope
3. Sub Main()
4. Console.WriteLine(" Scope of local varibale within a function")
5. local() ' call local() and local() function without any object reference
6. local2()
7. Console.WriteLine("press any key to exit...")
8. Console.ReadKey()
9. End Sub
10. Sub local()
11. Dim X As Integer
12. ' declaration of local variable
13. X = 50
14. Console.WriteLine(" Value of Local value X is {0}", X)
15.
16. End Sub
17. Sub local2()
18. Dim X As String
19. ' scope of local variable within a function
20. X = "JavaTpoint"
21. Console.WriteLine(" Value of X is {0}", X)
22. End Sub
23. End Module

Page | 13
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

Output:
Scope of local variable within a function
Value of Local value X is 50
Value of X is JavaTpoint
press any key to exit...

Module Scope
All existing procedures can easily identify a variable that is declared inside a module sheet
is called a module-level variable. The defined module variable is visible to all procedures within
that module only, but it is not available for other module's procedures. The Dim or private
statement at the top of the first procedure declaration can be declared the module-level
variables. It means that these variables cannot be declared inside any procedure block. Further,
these variables are useful to share information between the procedures in the same module. And
one more thing about the module-level variable is that these variables can remains existence as
long as the module is executed.
' It is the declaration section of the module
1. Private num As Integer ' A private module-level variable
2. Dim name As String ' Another private module-level variable

Let's create a program that display the module level variable in VB.NET.
Module_scope.vb
1. Imports System
2. Module Module_scope
3. 'module-level variable declaration
4. Dim x As Integer
5. Private y As Integer
6. Private name As String = "JavaTpoint"
7. Sub example()
8. x = 10
9. y = x + 10
10. Console.WriteLine(" Value of Y is {0}", y)
11. End Sub
12. Sub example2()
13. Console.WriteLine(" Value of X is {0}", x)
14. Console.WriteLine(" Value of Y is {0}", y)
15. Console.WriteLine(" Name is {0}", name)
16. End Sub
17. Sub example3()
Page | 14
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

18. Dim A As Integer ' local variable or local scope


19. A=x+y
20. Console.WriteLine(" Local scope within a function of variable A {0}", A)
21. End Sub
22. Sub Main()
23. Console.WriteLine(" Module scope of variable")
24. example()
25. example2()
26. example3()
27. Console.WriteLine("Press any key to exit...")
28. Console.ReadKey()
29. End Sub
30. End Module
Output:
Module scope of variable

Value of Y is 20
Value of X is 10
Value of Y is 20
Name is JavaTpoint
Local scope within a function of variable A 30
Press any key to exit...

Global (Public) Scope


As the name defines, a global variable is a variable that is used to access the
variables globally in a program. It means these variables can be accessed by all the procedures
or modules available in a program. To access the variables globally in a program, you need to
use the friend or public keyword with a variable in a module or class at the top of the first
procedure function. Global scope is also known as the Namespace scope.

Let's create a program that uses the global variable.


Global_scope1.vb
1. Imports System
2. Module Global_scope1
3. 'Global declaration of a variable
4. Public str As String = "Hello, Programmer."
5. Public topic As String
6. Public exp As Integer
7.
8. Sub Main()
Page | 15
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1

9. Console.WriteLine(" You have passed {0}", str)


10. Console.WriteLine(" Enter the topic name")
11. topic = Console.ReadLine
12. Console.WriteLine(" Topic Name :{0}", topic)
13. Console.WriteLine("How many years of experienced in {0}?", topic)
14. exp = Console.ReadLine
15. Console.WriteLine(" Your Experienced is {0} ", exp)
16. Console.ReadKey()
17. End Sub
18.
19. End Module
Output:
You have passed Hello, Programmer
Enter the topic name
VB.NET
Topic Name :VB.NET
How many years of experienced in VB.NET?
10
Your Experienced is 10

For more knowledge about constant, please check the link provided;
https://www.youtube.com/watch?v=6-hZZos24cQ

REFERENCES

https://www.javatpoint.com/vb-net-data-type

https://www.javatpoint.com/vb-net-variable-and-constant

Page | 16

You might also like