SlideShare a Scribd company logo
Constant
•Constant is a fixed value that does not change during the
program execution.
•You can define your own constant to use it in your program.
•Ex pi=3.14
Creating your own constants
• You can create your own constant to use it in your program.
• The value of the constant remains unchanged throughout the
program.
Example:
Private Sub cmdCalculate_Click()
Const pi = 3.1415926 'or Const pi As Double = 3.1415926
Dim area As Double, r As Double
r = 2
area = pi * r * r
Print area
End Sub
Output: 12.5663704
 NUMERIC CONSTANTS
Numbers are referred to as numeric constants in
Visual Basic
The following rules apply to numeric constants:
o Commas cannot appear anywhere in a numeric
constant.
o A numeric constant may be preceded by a + or
a − sign. The constant is understood to be
positive if a sign does not appear.
STRING CONSTANTS
A string constant is a sequence of characters (i.e.,
letters, numbers and certain special characters, such
as +, −, /, *, =, $, ., etc.), enclosed in quotation
marks. Blank spaces can be included within a string. A
quotation mark can also be placed within a string, but
it must be written as two adjacent quotation marks
String constants are used to represent nonnumeric
information, such as names, addresses, etc. There is no
practical restriction on the maximum number of characters
that can be included within a string constant. Thus, the
maximum length of a string constant can be considered
infinite.
Ex:
“19.95”
“hello ba jpcs”
“good morning, “studenta” ”
Variable
•Variable is used to store value.
•The value of the variable may vary during the program execution.
Naming Rules of variables
• A variable name must begin with an alphabet.
•It cannot be more than 255 characters.
•The variable name must not contain any special character like %,&,!,#,@ or $.
•And finally, it has to be unique within the same scope.
• Visual Basic does not distinguish between uppercase and lowercase letters. Many
programmers use uppercase letters as word separators within a single variable name (e.g.,
FreezingPoint, TaxRate, etc.)
• Visual Basic includes a number of reserved words (e.g., Dim, If, Else, Select, Case, Do, etc.).
These reserved words represent commands, function names, etc. They cannot be used as
variable names.
You can declare a variable with the 'dim' keyword.
Syntax:
Dim variable As [Type]
Example:
Private Sub cmdSum_Click()
Dim m As Integer
Dim n As Integer
Dim sum As Integer
m = 10 'm is a variable, 10 is a constant
n = 30
sum = m + n
Print "The sum is " & sum
End Sub
Output: The sum is 40
Variable Declaration
• Depending on where the variables are declared and how they are declared, there are many
ways to declare a variable in visual basic.
•When you declare a variable, memory space for the variable is reserved.
•This is called memory allocation.
•Different amount of memory space is reserved for different data types.
Variants
Visual Basic allows variables to be undeclared if the
programmer so chooses. In such cases, the data type of the
variable is determined implicitly by the value that is
assigned to the variable. Such variables are referred to as
Variant-type variables, or simply as variants.
Data Types
• Visual Basic is rich in its data types.
• Data types are used to declare the variables.
• At the time of declaration, memory is allocated for the variables.
• Different data types are used to store different types of values.
Data Type Storage Data Type Storage
Byte 1 byte
String (variable-
length)
Length + 10 bytes
Boolean 2 bytes
String (Fixed-
Length)
Length of string
Integer 2 bytes Currency 8 bytes
Long 4 bytes Decimal 12 bytes
Single 4 bytes Object 4 bytes
Double 8 bytes Variant (numeric) 16 bytes
Date 8 bytes Variant (text) length +22 bytes
Table: Memory storage for data types
Data Type Value Range
Byte 0 to 255
Boolean True/False
Integer -32,768 to 32,767
Long -2,147,483,648 to 2,147,483,647
Single
-3.402823*10^3 to -1.401298*10^45 for negative values
1.401298*10^-45 to 3.402823*10^38 for positive values
Double
-1.79*10^308 to -4.94*10^-324 for negative values
4.94*10^-324 to 1.79*10^308 for positive values
Date January 1, 100 to December 31, 9999
String (Variable length) 0 to approximately 2 billion characters
String (Fixed length) 1 to 65,400 characters
Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal
+,-79,228,162,514,264,337,593,543,950,335 if no decimal is used
+,-7.9228162514264337593543950335 (28 decimal places)
Object Any object
Variant (numeric) Any value as 16px as Double
Variant (text) Same as variable length string
Table: Data types & their value range
EXAMPLE OF DATA TYPE :
Use Integer data Type
Dim number As Interger
number=10
Dim NIC As Long
NIC=870740913
You can use any numeric values for this data
type.
Use String Data Type
Dim user_name As String
user_name="Hemantha“
Dim national_date As Date
national_date=1948/02/04
Use Boolean Data Type
Dim Do_you_love_me As Boolean
Do_you_love_me=True
Suffixes
Rather than declaring a data type explicitly (using a Dim or Const statement), a
variable or named constant can be associated with a data type by adding a single-
character suffix to the end of the variable/constant name. Several of the more
commonly used suffixes are listed below.
Suffix Data
Type
% integer
& long
integer
! single
# double
$ string
EXAMPLE 2.6
Shown below are several variables whose data types are defined
by suffixes.
Variable Data
Type
Index% integer
Counter& long
integer
TaxRate! single
Ratio# double
CustomerName$ string
Visual Basic Fundamentals

More Related Content

Similar to Visual Basic Fundamentals

programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
FatimaZafar68
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
Abhinav Vatsa
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
Kamal Acharya
 
PSPC--UNIT-2.pdf
PSPC--UNIT-2.pdfPSPC--UNIT-2.pdf
PSPC--UNIT-2.pdf
ArshiniGubbala3
 
UNIT-II VISUAL BASIC.NET | BCA
UNIT-II VISUAL BASIC.NET | BCAUNIT-II VISUAL BASIC.NET | BCA
UNIT-II VISUAL BASIC.NET | BCA
Raj vardhan
 
C programming language
C programming languageC programming language
C programming language
Abin Rimal
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
rohassanie
 
C Tutorial
C TutorialC Tutorial
C Tutorial
Dr.Subha Krishna
 
LESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptxLESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptx
joachimbenedicttulau
 
Mca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c languageMca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c language
Rai University
 
Lesson 4 Basic Programming Constructs.pptx
Lesson 4 Basic Programming Constructs.pptxLesson 4 Basic Programming Constructs.pptx
Lesson 4 Basic Programming Constructs.pptx
John Burca
 
Bsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c languageBsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c language
Rai University
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
Rai University
 
Btech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageBtech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c language
Rai University
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
Mohit Saini
 
C
CC
Diploma ii cfpc u-2 datatypes and variables in c language
Diploma ii  cfpc u-2 datatypes and variables in c languageDiploma ii  cfpc u-2 datatypes and variables in c language
Diploma ii cfpc u-2 datatypes and variables in c language
Rai University
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programming
Chitrank Dixit
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
Qazi Shahzad Ali
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
Rooney Joh
 

Similar to Visual Basic Fundamentals (20)

programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
PSPC--UNIT-2.pdf
PSPC--UNIT-2.pdfPSPC--UNIT-2.pdf
PSPC--UNIT-2.pdf
 
UNIT-II VISUAL BASIC.NET | BCA
UNIT-II VISUAL BASIC.NET | BCAUNIT-II VISUAL BASIC.NET | BCA
UNIT-II VISUAL BASIC.NET | BCA
 
C programming language
C programming languageC programming language
C programming language
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
C Tutorial
C TutorialC Tutorial
C Tutorial
 
LESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptxLESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptx
 
Mca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c languageMca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c language
 
Lesson 4 Basic Programming Constructs.pptx
Lesson 4 Basic Programming Constructs.pptxLesson 4 Basic Programming Constructs.pptx
Lesson 4 Basic Programming Constructs.pptx
 
Bsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c languageBsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c language
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Btech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageBtech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c language
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
 
C
CC
C
 
Diploma ii cfpc u-2 datatypes and variables in c language
Diploma ii  cfpc u-2 datatypes and variables in c languageDiploma ii  cfpc u-2 datatypes and variables in c language
Diploma ii cfpc u-2 datatypes and variables in c language
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programming
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 

Recently uploaded

GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 

Recently uploaded (20)

GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 

Visual Basic Fundamentals

  • 1.
  • 2. Constant •Constant is a fixed value that does not change during the program execution. •You can define your own constant to use it in your program. •Ex pi=3.14 Creating your own constants • You can create your own constant to use it in your program. • The value of the constant remains unchanged throughout the program.
  • 3. Example: Private Sub cmdCalculate_Click() Const pi = 3.1415926 'or Const pi As Double = 3.1415926 Dim area As Double, r As Double r = 2 area = pi * r * r Print area End Sub Output: 12.5663704  NUMERIC CONSTANTS Numbers are referred to as numeric constants in Visual Basic The following rules apply to numeric constants: o Commas cannot appear anywhere in a numeric constant. o A numeric constant may be preceded by a + or a − sign. The constant is understood to be positive if a sign does not appear.
  • 4. STRING CONSTANTS A string constant is a sequence of characters (i.e., letters, numbers and certain special characters, such as +, −, /, *, =, $, ., etc.), enclosed in quotation marks. Blank spaces can be included within a string. A quotation mark can also be placed within a string, but it must be written as two adjacent quotation marks String constants are used to represent nonnumeric information, such as names, addresses, etc. There is no practical restriction on the maximum number of characters that can be included within a string constant. Thus, the maximum length of a string constant can be considered infinite. Ex: “19.95” “hello ba jpcs” “good morning, “studenta” ”
  • 5. Variable •Variable is used to store value. •The value of the variable may vary during the program execution. Naming Rules of variables • A variable name must begin with an alphabet. •It cannot be more than 255 characters. •The variable name must not contain any special character like %,&,!,#,@ or $. •And finally, it has to be unique within the same scope. • Visual Basic does not distinguish between uppercase and lowercase letters. Many programmers use uppercase letters as word separators within a single variable name (e.g., FreezingPoint, TaxRate, etc.) • Visual Basic includes a number of reserved words (e.g., Dim, If, Else, Select, Case, Do, etc.). These reserved words represent commands, function names, etc. They cannot be used as variable names.
  • 6. You can declare a variable with the 'dim' keyword. Syntax: Dim variable As [Type] Example: Private Sub cmdSum_Click() Dim m As Integer Dim n As Integer Dim sum As Integer m = 10 'm is a variable, 10 is a constant n = 30 sum = m + n Print "The sum is " & sum End Sub Output: The sum is 40 Variable Declaration • Depending on where the variables are declared and how they are declared, there are many ways to declare a variable in visual basic. •When you declare a variable, memory space for the variable is reserved. •This is called memory allocation. •Different amount of memory space is reserved for different data types.
  • 7. Variants Visual Basic allows variables to be undeclared if the programmer so chooses. In such cases, the data type of the variable is determined implicitly by the value that is assigned to the variable. Such variables are referred to as Variant-type variables, or simply as variants. Data Types • Visual Basic is rich in its data types. • Data types are used to declare the variables. • At the time of declaration, memory is allocated for the variables. • Different data types are used to store different types of values.
  • 8. Data Type Storage Data Type Storage Byte 1 byte String (variable- length) Length + 10 bytes Boolean 2 bytes String (Fixed- Length) Length of string Integer 2 bytes Currency 8 bytes Long 4 bytes Decimal 12 bytes Single 4 bytes Object 4 bytes Double 8 bytes Variant (numeric) 16 bytes Date 8 bytes Variant (text) length +22 bytes Table: Memory storage for data types
  • 9. Data Type Value Range Byte 0 to 255 Boolean True/False Integer -32,768 to 32,767 Long -2,147,483,648 to 2,147,483,647 Single -3.402823*10^3 to -1.401298*10^45 for negative values 1.401298*10^-45 to 3.402823*10^38 for positive values Double -1.79*10^308 to -4.94*10^-324 for negative values 4.94*10^-324 to 1.79*10^308 for positive values Date January 1, 100 to December 31, 9999 String (Variable length) 0 to approximately 2 billion characters String (Fixed length) 1 to 65,400 characters Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807 Decimal +,-79,228,162,514,264,337,593,543,950,335 if no decimal is used +,-7.9228162514264337593543950335 (28 decimal places) Object Any object Variant (numeric) Any value as 16px as Double Variant (text) Same as variable length string Table: Data types & their value range
  • 10. EXAMPLE OF DATA TYPE : Use Integer data Type Dim number As Interger number=10 Dim NIC As Long NIC=870740913 You can use any numeric values for this data type. Use String Data Type Dim user_name As String user_name="Hemantha“ Dim national_date As Date national_date=1948/02/04 Use Boolean Data Type Dim Do_you_love_me As Boolean Do_you_love_me=True
  • 11. Suffixes Rather than declaring a data type explicitly (using a Dim or Const statement), a variable or named constant can be associated with a data type by adding a single- character suffix to the end of the variable/constant name. Several of the more commonly used suffixes are listed below. Suffix Data Type % integer & long integer ! single # double $ string
  • 12. EXAMPLE 2.6 Shown below are several variables whose data types are defined by suffixes. Variable Data Type Index% integer Counter& long integer TaxRate! single Ratio# double CustomerName$ string