SlideShare a Scribd company logo
5
Most read
10
Most read
11
Most read
VB.NET:- Variable and Constants
BCA -501
2
Content
 Variable
 Variable Declarations
 Variable Declaration Example
 Variable Initialisations
 Getting Values from user
 Lvalues & Rvalues
 Constants
 Enumeration
 Scope of Variable
3
Variable
 A variable is used to hold the value that can be used further in the programming.
 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.
4
Variable Declarations
The Dim statement is used for variable declaration and storage allocation for one or more
variables. The Dim statement is used at module, class, structure, procedure or block level. .
Each variable in the variable list has the following syntax and parts −
variablename[ ( [ boundslist ] ) ] [ As [ New ] datatype ] [ = initializer ]
Where,
 variablename − is the name of the variable
 boundslist − optional. It provides list of bounds of each dimension of an array variable.
 New − optional. It creates a new instance of the class when the Dim statement runs.
 datatype − Required if Option Strict is On. It specifies the data type of the variable.
 initializer − Optional if New is not specified. Expression that is evaluated and assigned to the
variable when it is created.
5
Variable Declarations Syntax
Syntax: 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.
6
Variable Declarations Example
Dim Roll_no As Integer
Dim Emp_name As String
Dim Salary As Double
Dim Emp_id, Stud_id As Integer
Dim result_status As Boolean
Note:
If we want to declare more than one variable in the same line, we must separate each variable with a comma.
Dim Variable_name1 As DataType1, variable_name2 As DataType2, Variable_name3 As DataType3
7
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: Variable_name = value
Dim Roll_no As Integer 'declaration of Roll_no
Roll_no = 101 'initialization of Roll_no
Initialize the Emp_name
Dim Emp_name As String
Emp_name = “Jaya" 'Here Emp_name variable assigned a value of Jaya
Initialize a Boolean variable
Dim status As Boolean 'Boolean value can be True or False.
status = True 'Initialize status value to True
Initialzation at the time of declaration:
Dim Roll_no As Integer = 101
Dim Emp_name As String = " Stephen Robert "
8
Getting Values from the User:
 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
Dim name As String
name = Console.ReadLine()
Or name = Console.ReadLine
Imports System
Module User_Data
Sub Main()
Dim num As Integer
Dim age As Double
Dim name As String
Console.WriteLine("Enter your favourite number")
' Console.ReadLine or Console.ReadLine() takes value from the user
num = Console.ReadLine
Console.WriteLine(" Enter Your Good name")
'Read string data from the user
name = Console.ReadLine
Console.WriteLine(" Enter your Age")
age = Console.ReadLine
Console.WriteLine(" You have entered {0}", num)
Console.WriteLine(" You have entered {0}", name)
Console.WriteLine(" You have entered {0}", age)
Console.ReadKey()
End Sub
End Module
Note: Console.Read() and
Console.ReadKey() function is
used to read a single character
from the user.
9
Lvalues & Rvalues
 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.
Dim num As Integer
Num = 5
Or
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.
Dim x As Integer
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.
Dim college_name As String
college_name = “ISM" // rvalue define at right side of the assignment operator.
10
Constants in VB.NET
 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.
 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.
Const constname As datatype = value
Item Name Descriptions
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.
11
Print and Display Constant
Const Name Descriptions
vbCrLf Carriage return/linefeed character combination
vbCr Carriage return character
vbLf Linefeed character
vbNewLine Newline character
vbNullChar Null character
vbNullString Not the same as a zero-length string (""); used for calling external procedures.
vbObjectError Error number. User-defined error numbers should be greater than this value. For example:
Err.Raise(Number) = vbObjectError + 1000
vbTab Tab character.
vbBack Backspace character.
12
Enumerations in VB.NET
 An enumerated type is declared using the Enum statement.
 The Enum statement declares an enumeration and defines the values of its members.
 The Enum statement can be used at the module, class, structure, procedure, or block level.
Each member in the memberlist has the following syntax and
parts:
[< attribute list >] member name [ = initializer ]
Where,
name − specifies the name of the member. Required.
initializer − value assigned to the enumeration member. Optional.
Example:
Enum Colors
red = 1
orange = 2
yellow = 3
green = 4
azure = 5
blue = 6
violet = 7
End Enum
13
Print and Display Constant
Const Name Descriptions
vbCrLf Carriage return/linefeed character combination
vbCr Carriage return character
vbLf Linefeed character
vbNewLine Newline character
vbNullChar Null character
vbNullString Not the same as a zero-length string (""); used for calling external procedures.
vbObjectError Error number. User-defined error numbers should be greater than this value. For example:
Err.Raise(Number) = vbObjectError + 1000
vbTab Tab character.
vbBack Backspace character.
14
Scope of Variables
The scope of a variable determines the accessible range of a defined variable at the time of
declaration in any block, module, and class.
It can be accessed, 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
3. Public Scope
15
Procedure 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.
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.
16
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.
17
Global 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, we 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.
'Global declaration of a variable
Public str As String = "Hello, Programmer."
Public topic As String
Public exp As Integer

More Related Content

PPT
Vb introduction.
PPT
VB.net
PPTX
Classes and objects
PPT
Visual programming lecture
PPTX
Introduction to vb.net
PPT
Introduction to visual basic programming
PPTX
Basic controls of Visual Basic 6.0
PPT
File organization and indexing
Vb introduction.
VB.net
Classes and objects
Visual programming lecture
Introduction to vb.net
Introduction to visual basic programming
Basic controls of Visual Basic 6.0
File organization and indexing

What's hot (20)

PDF
Python Flow Control
PPTX
Operators used in vb.net
PPTX
Control statements in java
PPTX
Windowforms controls c#
PPTX
Packages in java
PPTX
Java package
PDF
Strings in java
PPS
Procedures functions structures in VB.Net
PDF
Class and Objects in Java
PPTX
Operators in java
PPTX
Constants in java
PPTX
Windows form application_in_vb(vb.net --3 year)
PDF
Asp.net state management
PPT
RichControl in Asp.net
PPTX
Control structures in java
PPSX
Control Structures in Visual Basic
PPTX
arrays and pointers
PDF
Files and streams
PPT
Applet life cycle
Python Flow Control
Operators used in vb.net
Control statements in java
Windowforms controls c#
Packages in java
Java package
Strings in java
Procedures functions structures in VB.Net
Class and Objects in Java
Operators in java
Constants in java
Windows form application_in_vb(vb.net --3 year)
Asp.net state management
RichControl in Asp.net
Control structures in java
Control Structures in Visual Basic
arrays and pointers
Files and streams
Applet life cycle
Ad

Similar to Variable and constants in Vb.NET (20)

PPTX
CHAPTER 2 (Data types,Variables) in Visual Basic Programming
PDF
VB PPT by ADI PART2.pdf
PDF
VB PPT by ADI PART2.pdf
PPTX
Variable scope ppt in vb6
PPT
Ch (2)(presentation) its about visual programming
PPTX
Variables and calculations_chpt_4
PPTX
CHAPTER 3- Lesson A
PPTX
Visual Basic Fundamentals
PPT
Ppt lesson 05
PPT
Lesson 5 PP
PPT
Intro_to_VB.PPT
PPT
Visual basics
PPTX
Visual Programming
PDF
Visual Basics for Application
PPTX
Unit 1 introduction to visual basic programming
PPTX
E learning excel vba programming lesson 3
PPT
Qtp - Introduction to fundamentals of vbscript
PPTX
Unit 5: Variables
PPS
Visual Basic Review - ICA
CHAPTER 2 (Data types,Variables) in Visual Basic Programming
VB PPT by ADI PART2.pdf
VB PPT by ADI PART2.pdf
Variable scope ppt in vb6
Ch (2)(presentation) its about visual programming
Variables and calculations_chpt_4
CHAPTER 3- Lesson A
Visual Basic Fundamentals
Ppt lesson 05
Lesson 5 PP
Intro_to_VB.PPT
Visual basics
Visual Programming
Visual Basics for Application
Unit 1 introduction to visual basic programming
E learning excel vba programming lesson 3
Qtp - Introduction to fundamentals of vbscript
Unit 5: Variables
Visual Basic Review - ICA
Ad

More from Jaya Kumari (19)

PPTX
Python data type
PPTX
Introduction to python
PPTX
Variables in python
PPTX
Basic syntax supported by python
PPTX
PPTX
Inheritance
PPTX
Overloading
PPTX
Decision statements
PPTX
Loop control statements
PPTX
Looping statements
PPTX
Keywords, identifiers and data type of vb.net
PPTX
Frame class library and namespace
PPTX
Introduction to .net
PPTX
Jsp basic
PPTX
Java script Basic
PPTX
Sgml and xml
PPTX
Java script Advance
PPTX
Html form
PPTX
Html Concept
Python data type
Introduction to python
Variables in python
Basic syntax supported by python
Inheritance
Overloading
Decision statements
Loop control statements
Looping statements
Keywords, identifiers and data type of vb.net
Frame class library and namespace
Introduction to .net
Jsp basic
Java script Basic
Sgml and xml
Java script Advance
Html form
Html Concept

Recently uploaded (20)

PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Presentation on HIE in infants and its manifestations
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Structure & Organelles in detailed.
PPTX
Cell Types and Its function , kingdom of life
PPTX
Lesson notes of climatology university.
PPTX
Institutional Correction lecture only . . .
PDF
Computing-Curriculum for Schools in Ghana
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Anesthesia in Laparoscopic Surgery in India
Presentation on HIE in infants and its manifestations
Supply Chain Operations Speaking Notes -ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
2.FourierTransform-ShortQuestionswithAnswers.pdf
GDM (1) (1).pptx small presentation for students
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
Complications of Minimal Access Surgery at WLH
O5-L3 Freight Transport Ops (International) V1.pdf
O7-L3 Supply Chain Operations - ICLT Program
VCE English Exam - Section C Student Revision Booklet
Microbial diseases, their pathogenesis and prophylaxis
Cell Structure & Organelles in detailed.
Cell Types and Its function , kingdom of life
Lesson notes of climatology university.
Institutional Correction lecture only . . .
Computing-Curriculum for Schools in Ghana

Variable and constants in Vb.NET

  • 1. VB.NET:- Variable and Constants BCA -501
  • 2. 2 Content  Variable  Variable Declarations  Variable Declaration Example  Variable Initialisations  Getting Values from user  Lvalues & Rvalues  Constants  Enumeration  Scope of Variable
  • 3. 3 Variable  A variable is used to hold the value that can be used further in the programming.  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.
  • 4. 4 Variable Declarations The Dim statement is used for variable declaration and storage allocation for one or more variables. The Dim statement is used at module, class, structure, procedure or block level. . Each variable in the variable list has the following syntax and parts − variablename[ ( [ boundslist ] ) ] [ As [ New ] datatype ] [ = initializer ] Where,  variablename − is the name of the variable  boundslist − optional. It provides list of bounds of each dimension of an array variable.  New − optional. It creates a new instance of the class when the Dim statement runs.  datatype − Required if Option Strict is On. It specifies the data type of the variable.  initializer − Optional if New is not specified. Expression that is evaluated and assigned to the variable when it is created.
  • 5. 5 Variable Declarations Syntax Syntax: 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.
  • 6. 6 Variable Declarations Example Dim Roll_no As Integer Dim Emp_name As String Dim Salary As Double Dim Emp_id, Stud_id As Integer Dim result_status As Boolean Note: If we want to declare more than one variable in the same line, we must separate each variable with a comma. Dim Variable_name1 As DataType1, variable_name2 As DataType2, Variable_name3 As DataType3
  • 7. 7 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: Variable_name = value Dim Roll_no As Integer 'declaration of Roll_no Roll_no = 101 'initialization of Roll_no Initialize the Emp_name Dim Emp_name As String Emp_name = “Jaya" 'Here Emp_name variable assigned a value of Jaya Initialize a Boolean variable Dim status As Boolean 'Boolean value can be True or False. status = True 'Initialize status value to True Initialzation at the time of declaration: Dim Roll_no As Integer = 101 Dim Emp_name As String = " Stephen Robert "
  • 8. 8 Getting Values from the User:  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 Dim name As String name = Console.ReadLine() Or name = Console.ReadLine Imports System Module User_Data Sub Main() Dim num As Integer Dim age As Double Dim name As String Console.WriteLine("Enter your favourite number") ' Console.ReadLine or Console.ReadLine() takes value from the user num = Console.ReadLine Console.WriteLine(" Enter Your Good name") 'Read string data from the user name = Console.ReadLine Console.WriteLine(" Enter your Age") age = Console.ReadLine Console.WriteLine(" You have entered {0}", num) Console.WriteLine(" You have entered {0}", name) Console.WriteLine(" You have entered {0}", age) Console.ReadKey() End Sub End Module Note: Console.Read() and Console.ReadKey() function is used to read a single character from the user.
  • 9. 9 Lvalues & Rvalues  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. Dim num As Integer Num = 5 Or 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. Dim x As Integer 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. Dim college_name As String college_name = “ISM" // rvalue define at right side of the assignment operator.
  • 10. 10 Constants in VB.NET  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.  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. Const constname As datatype = value Item Name Descriptions 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.
  • 11. 11 Print and Display Constant Const Name Descriptions vbCrLf Carriage return/linefeed character combination vbCr Carriage return character vbLf Linefeed character vbNewLine Newline character vbNullChar Null character vbNullString Not the same as a zero-length string (""); used for calling external procedures. vbObjectError Error number. User-defined error numbers should be greater than this value. For example: Err.Raise(Number) = vbObjectError + 1000 vbTab Tab character. vbBack Backspace character.
  • 12. 12 Enumerations in VB.NET  An enumerated type is declared using the Enum statement.  The Enum statement declares an enumeration and defines the values of its members.  The Enum statement can be used at the module, class, structure, procedure, or block level. Each member in the memberlist has the following syntax and parts: [< attribute list >] member name [ = initializer ] Where, name − specifies the name of the member. Required. initializer − value assigned to the enumeration member. Optional. Example: Enum Colors red = 1 orange = 2 yellow = 3 green = 4 azure = 5 blue = 6 violet = 7 End Enum
  • 13. 13 Print and Display Constant Const Name Descriptions vbCrLf Carriage return/linefeed character combination vbCr Carriage return character vbLf Linefeed character vbNewLine Newline character vbNullChar Null character vbNullString Not the same as a zero-length string (""); used for calling external procedures. vbObjectError Error number. User-defined error numbers should be greater than this value. For example: Err.Raise(Number) = vbObjectError + 1000 vbTab Tab character. vbBack Backspace character.
  • 14. 14 Scope of Variables The scope of a variable determines the accessible range of a defined variable at the time of declaration in any block, module, and class. It can be accessed, 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 3. Public Scope
  • 15. 15 Procedure 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. 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.
  • 16. 16 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.
  • 17. 17 Global 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, we 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. 'Global declaration of a variable Public str As String = "Hello, Programmer." Public topic As String Public exp As Integer