SlideShare a Scribd company logo
1 of 13
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.pptFatimaZafar68
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic conceptsAbhinav 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 ProgrammingKamal Acharya
 
UNIT-II VISUAL BASIC.NET | BCA
UNIT-II VISUAL BASIC.NET | BCAUNIT-II VISUAL BASIC.NET | BCA
UNIT-II VISUAL BASIC.NET | BCARaj vardhan
 
C programming language
C programming languageC programming language
C programming languageAbin Rimal
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2rohassanie
 
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 languageRai University
 
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 languageRai University
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c languageRai 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 languageRai University
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorialMohit Saini
 
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 languageRai University
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programmingChitrank Dixit
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 

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
 
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
 
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
 
Data type
Data typeData type
Data type
 
lec 2.pptx
lec 2.pptxlec 2.pptx
lec 2.pptx
 

Recently uploaded

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 

Recently uploaded (20)

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 

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