SlideShare a Scribd company logo
1 of 55
Hello!
I am Ma. Wennilou Porazo
Instructor
1
Using
Variables and
Constants
After studying Lesson A, you should
be able to:
● Declare variables and named constants
● Assign data to an existing variable
● Convert string data to a numeric data type using the
TryParse method
3
After studying Lesson A, you should
be able to:
● Convert numeric data to a different data type using
the Convert class methods
● Explain the scope and lifetime of variables and named
constants
● Explain the purpose of Option Explicit, Option Infer,
and Option Strict
4
Using Variables to Store Information
● Controls and variables temporarily store data
● Variable
○ A temporary storage location in main memory
○ Specified by data type, name, scope, and
lifetime
5
Using Variables to Store Information
● Reasons to use variables
○ To hold information that is not stored in a
control on the form.
○ To allow for more precise treatment of
numeric data.
○ To enable code to run more efficiently
6
Using Variables to Store Information (cont.)
● Selecting a Data Type for a Variable
● Data type
○ Specifies the type of data a variable can store
○ Provides a class template for creating
variables
7
Using Variables to Store Information (cont.)
● Unicode
○ A universal coding scheme for characters
○ Assigns a unique numeric value to each
character in the written languages of the
world
8
9
10
Using Variables to Store Information (cont.)
● The textbook uses:
○ The Integer data type for all integers
○ Either the Decimal or Double data type for
numbers containing decimal places or
numbers used in calculations
11
Using Variables to Store Information (cont.)
● The String data type for text or numbers not
used in calculations
● The Boolean data type for Boolean values
12
Using Variables to Store Information (cont.)
● Selecting a Name for a Variable
○ Names must begin with a letter or underscore
○ Names can contain only letters, numbers, or
underscores
○ No punctuation, special characters, or spaces
are allowed
13
Using Variables to Store Information (cont.)
● The recommended length for a name variable is
32 characters
● Variable names cannot be reserved words (such
as Sub or Double)
14
Using Variables to Store Information (cont.)
● Validnames:
○ intFeb_Income
○ decSales2014
○ dblEastRegion
○ strName
15
Using Variables to Store Information (cont.)
● Invalid Names:
○ 04thQuarter
■ The name must begin with a letter or
underscore
○ dblWest Region
■ The name cannot contain a space
16
Using Variables to Store Information (cont.)
○ strFirst.Name
■ The name cannot contain punctuation
○ decSales$East
■ The name cannot contain a special
character
17
Using Variables to Store Information (cont.)
● Declaring a Variable
● 1. Declaration statement
○ Used to declare (create) a variable and
reserve space in memory for it
18
Using Variables to Store Information (cont.)
● 2. If no initial value is given to a variable when
declaring it, the computer stores a default value
○ Numeric variables are set to 0
○ Boolean variables are set to False
○ Object and String variables are set to Nothing
○ Date variables are set to 1/1/ :00:00AM
19
20
Assigning Data to an Existing
Variable
● 1. Assignment statement
○ Assigns a value to a variable at run time
○ Syntax: variablename = expression
○ An expression may include literal constants,
object properties, variables, keywords, and
arithmetic operators
21
22
Assigning Data to an Existing Variable
● 2. Literal constant
○ A data item whose value does not change while the
application is running
○ Example: The string “Mary”
● 3. Literal type character
○ Forces a literal constant to change its data type
23
Assigning Data to an Existing Variable (cont.)
The TryParse Method
● 1. Converts a string to a number
● 2. Is preferred over Val
○ Allows the programmer to specify the data
type
○ Val only returns a Double number
24
Assigning Data to an Existing Variable (cont.)
● 3. Arguments
○ dataType: A numeric data type, such as
Integer
○ String: A string to be converted
○ Variable: A variable that receives the numeric
value
25
26
27
28
The Convert Class
● 1. Can be used to convert a number from one
type to another
● 2. Methods include ToDecimal, ToDouble,
ToInt32, and ToString
29
● 3. TryParse is recommended for converting
strings to numeric data types
○ Will not produce an error if the conversion
failsProgramming
30
31
32
The Scope and Lifetime of a Variable
● Scope
○ Indicates where a variable can be used
● Lifetime
○ How long a variable remains in memory
● Scope and lifetime are determined by where a variable is
declared: either the General Declarations section or the
form’s Declaration section
33
The Scope and Lifetime of a Variable
● Three types of scope:
○ Class: The variable can be used by all procedures in a
form
○ Procedure: The variable can be used within a
procedure
○ Block: The variable can be used within a specific code
block
34
The Scope and Lifetime of a Variable
(cont.)
● Variables with Procedure Scope
○ Can be used only by that procedure
○ Declared at the beginning of the procedure
○ Removed from memory when the procedure ends
○ Declared using the Dim keyword
35
The Scope and Lifetime of a Variable
(cont.)
● Most variables used
in this course will be
procedure-level
variables
36
37
Variable with Class Scope
● Can be used by all procedures in the
form
● Declared in the form’s Declarations
section
● Will remain in memory until the
application ends
● Declared using the Private keyword
38
39
Static Variables
Static Variable
● 1. A procedure-level variable with an extended
lifetime
○ Remains in memory between procedure calls
○ Retains its value even when the procedure
ends
40
Static Variables
● 2. Static keyword
○ Used to declare a static variable
● 3. Static variables act like class-level variables but
have narrower scope
○ They can only be used by the procedure in
which they are declared
41
42
Named Constants
● Named Constant
○ A memory location inside the computer whose
contents cannot be changed at run time
43
Named Constants (cont.)
● Const statement
○ Creates named constant
○ Stores value of expression in a named constant
○ expression: Can be a literal constant, another named
constant, or an arithmetic operator
○ Cannot contain a variable or method
44
45
46
47
Option Statements
● Option Explicit and Option Infer
○ Prevent you from using undeclared variables
● Option Strict
○ 1. Implicit type conversion
■ Converts the right-side value to the data
type on the left side
48
Option Statements
● 2. Promotion
○ Data is converted to a greater precision number (e.g., Integer
to Decimal)
● 3. Demotion
○ Data is truncated (e.g., Decimal to Integer)
○ Data loss can occur when demotion occurs
● 4. Infer
○ Ensures that every variable is declared with a data type
49
Option Statement (cont.)
● Option Strict (cont.)
○ Disallows implicit conversions
○ Type conversion rules are applied when this
option is on.
50
51
52
Lesson A Summary
● Declare variable using {Dim | Private |Static}
● An assignment statement assigns a value to a variable
● Three levels of scope: block, procedure, class
● The TryParse method converts strings to numeric data
● Use Const to declare a named constant
● Avoid programming errors by using Option Explicit On,
Option Infer Off, and Option Strict On
53
Reference
● Book
○ Programming with Microsoft Visual Basic
2010, 5th edition
○ Author: Diane Zak
54
Thanks!
55

More Related Content

Similar to CHAPTER 3- Lesson A

Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
eShikshak
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdf
YRABHI
 
The presentation of Type4Py at the ICSE'22 conference
The presentation of Type4Py at the ICSE'22 conferenceThe presentation of Type4Py at the ICSE'22 conference
The presentation of Type4Py at the ICSE'22 conference
Amir M. Mir
 

Similar to CHAPTER 3- Lesson A (20)

Data types vbnet
Data types vbnetData types vbnet
Data types vbnet
 
Model Fields in Odoo 15
 Model Fields in Odoo 15 Model Fields in Odoo 15
Model Fields in Odoo 15
 
Variables.pdf
Variables.pdfVariables.pdf
Variables.pdf
 
What are variables and keywords in c++
What are variables and keywords in c++What are variables and keywords in c++
What are variables and keywords in c++
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
different Data type Modifiers in c language
different Data type Modifiers in c languagedifferent Data type Modifiers in c language
different Data type Modifiers in c language
 
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdfLec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
 
Lecture 4 constants_variables
Lecture 4 constants_variablesLecture 4 constants_variables
Lecture 4 constants_variables
 
Visual Basic Fundamentals
Visual Basic FundamentalsVisual Basic Fundamentals
Visual Basic Fundamentals
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
PF 2- Data Types and Veriables (1).pptx
PF 2- Data Types and Veriables (1).pptxPF 2- Data Types and Veriables (1).pptx
PF 2- Data Types and Veriables (1).pptx
 
PLPgSqL- Datatypes, Language structure.pptx
PLPgSqL- Datatypes, Language structure.pptxPLPgSqL- Datatypes, Language structure.pptx
PLPgSqL- Datatypes, Language structure.pptx
 
Week 08 - Identifier.pdf
Week 08 - Identifier.pdfWeek 08 - Identifier.pdf
Week 08 - Identifier.pdf
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdf
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
The presentation of Type4Py at the ICSE'22 conference
The presentation of Type4Py at the ICSE'22 conferenceThe presentation of Type4Py at the ICSE'22 conference
The presentation of Type4Py at the ICSE'22 conference
 
COM1407: Variables and Data Types
COM1407: Variables and Data Types COM1407: Variables and Data Types
COM1407: Variables and Data Types
 

More from MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Recently uploaded

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

CHAPTER 3- Lesson A

  • 1. Hello! I am Ma. Wennilou Porazo Instructor 1
  • 3. After studying Lesson A, you should be able to: ● Declare variables and named constants ● Assign data to an existing variable ● Convert string data to a numeric data type using the TryParse method 3
  • 4. After studying Lesson A, you should be able to: ● Convert numeric data to a different data type using the Convert class methods ● Explain the scope and lifetime of variables and named constants ● Explain the purpose of Option Explicit, Option Infer, and Option Strict 4
  • 5. Using Variables to Store Information ● Controls and variables temporarily store data ● Variable ○ A temporary storage location in main memory ○ Specified by data type, name, scope, and lifetime 5
  • 6. Using Variables to Store Information ● Reasons to use variables ○ To hold information that is not stored in a control on the form. ○ To allow for more precise treatment of numeric data. ○ To enable code to run more efficiently 6
  • 7. Using Variables to Store Information (cont.) ● Selecting a Data Type for a Variable ● Data type ○ Specifies the type of data a variable can store ○ Provides a class template for creating variables 7
  • 8. Using Variables to Store Information (cont.) ● Unicode ○ A universal coding scheme for characters ○ Assigns a unique numeric value to each character in the written languages of the world 8
  • 9. 9
  • 10. 10
  • 11. Using Variables to Store Information (cont.) ● The textbook uses: ○ The Integer data type for all integers ○ Either the Decimal or Double data type for numbers containing decimal places or numbers used in calculations 11
  • 12. Using Variables to Store Information (cont.) ● The String data type for text or numbers not used in calculations ● The Boolean data type for Boolean values 12
  • 13. Using Variables to Store Information (cont.) ● Selecting a Name for a Variable ○ Names must begin with a letter or underscore ○ Names can contain only letters, numbers, or underscores ○ No punctuation, special characters, or spaces are allowed 13
  • 14. Using Variables to Store Information (cont.) ● The recommended length for a name variable is 32 characters ● Variable names cannot be reserved words (such as Sub or Double) 14
  • 15. Using Variables to Store Information (cont.) ● Validnames: ○ intFeb_Income ○ decSales2014 ○ dblEastRegion ○ strName 15
  • 16. Using Variables to Store Information (cont.) ● Invalid Names: ○ 04thQuarter ■ The name must begin with a letter or underscore ○ dblWest Region ■ The name cannot contain a space 16
  • 17. Using Variables to Store Information (cont.) ○ strFirst.Name ■ The name cannot contain punctuation ○ decSales$East ■ The name cannot contain a special character 17
  • 18. Using Variables to Store Information (cont.) ● Declaring a Variable ● 1. Declaration statement ○ Used to declare (create) a variable and reserve space in memory for it 18
  • 19. Using Variables to Store Information (cont.) ● 2. If no initial value is given to a variable when declaring it, the computer stores a default value ○ Numeric variables are set to 0 ○ Boolean variables are set to False ○ Object and String variables are set to Nothing ○ Date variables are set to 1/1/ :00:00AM 19
  • 20. 20
  • 21. Assigning Data to an Existing Variable ● 1. Assignment statement ○ Assigns a value to a variable at run time ○ Syntax: variablename = expression ○ An expression may include literal constants, object properties, variables, keywords, and arithmetic operators 21
  • 22. 22 Assigning Data to an Existing Variable ● 2. Literal constant ○ A data item whose value does not change while the application is running ○ Example: The string “Mary” ● 3. Literal type character ○ Forces a literal constant to change its data type
  • 23. 23
  • 24. Assigning Data to an Existing Variable (cont.) The TryParse Method ● 1. Converts a string to a number ● 2. Is preferred over Val ○ Allows the programmer to specify the data type ○ Val only returns a Double number 24
  • 25. Assigning Data to an Existing Variable (cont.) ● 3. Arguments ○ dataType: A numeric data type, such as Integer ○ String: A string to be converted ○ Variable: A variable that receives the numeric value 25
  • 26. 26
  • 27. 27
  • 28. 28
  • 29. The Convert Class ● 1. Can be used to convert a number from one type to another ● 2. Methods include ToDecimal, ToDouble, ToInt32, and ToString 29
  • 30. ● 3. TryParse is recommended for converting strings to numeric data types ○ Will not produce an error if the conversion failsProgramming 30
  • 31. 31
  • 32. 32
  • 33. The Scope and Lifetime of a Variable ● Scope ○ Indicates where a variable can be used ● Lifetime ○ How long a variable remains in memory ● Scope and lifetime are determined by where a variable is declared: either the General Declarations section or the form’s Declaration section 33
  • 34. The Scope and Lifetime of a Variable ● Three types of scope: ○ Class: The variable can be used by all procedures in a form ○ Procedure: The variable can be used within a procedure ○ Block: The variable can be used within a specific code block 34
  • 35. The Scope and Lifetime of a Variable (cont.) ● Variables with Procedure Scope ○ Can be used only by that procedure ○ Declared at the beginning of the procedure ○ Removed from memory when the procedure ends ○ Declared using the Dim keyword 35
  • 36. The Scope and Lifetime of a Variable (cont.) ● Most variables used in this course will be procedure-level variables 36
  • 37. 37
  • 38. Variable with Class Scope ● Can be used by all procedures in the form ● Declared in the form’s Declarations section ● Will remain in memory until the application ends ● Declared using the Private keyword 38
  • 39. 39
  • 40. Static Variables Static Variable ● 1. A procedure-level variable with an extended lifetime ○ Remains in memory between procedure calls ○ Retains its value even when the procedure ends 40
  • 41. Static Variables ● 2. Static keyword ○ Used to declare a static variable ● 3. Static variables act like class-level variables but have narrower scope ○ They can only be used by the procedure in which they are declared 41
  • 42. 42
  • 43. Named Constants ● Named Constant ○ A memory location inside the computer whose contents cannot be changed at run time 43
  • 44. Named Constants (cont.) ● Const statement ○ Creates named constant ○ Stores value of expression in a named constant ○ expression: Can be a literal constant, another named constant, or an arithmetic operator ○ Cannot contain a variable or method 44
  • 45. 45
  • 46. 46
  • 47. 47
  • 48. Option Statements ● Option Explicit and Option Infer ○ Prevent you from using undeclared variables ● Option Strict ○ 1. Implicit type conversion ■ Converts the right-side value to the data type on the left side 48
  • 49. Option Statements ● 2. Promotion ○ Data is converted to a greater precision number (e.g., Integer to Decimal) ● 3. Demotion ○ Data is truncated (e.g., Decimal to Integer) ○ Data loss can occur when demotion occurs ● 4. Infer ○ Ensures that every variable is declared with a data type 49
  • 50. Option Statement (cont.) ● Option Strict (cont.) ○ Disallows implicit conversions ○ Type conversion rules are applied when this option is on. 50
  • 51. 51
  • 52. 52
  • 53. Lesson A Summary ● Declare variable using {Dim | Private |Static} ● An assignment statement assigns a value to a variable ● Three levels of scope: block, procedure, class ● The TryParse method converts strings to numeric data ● Use Const to declare a named constant ● Avoid programming errors by using Option Explicit On, Option Infer Off, and Option Strict On 53
  • 54. Reference ● Book ○ Programming with Microsoft Visual Basic 2010, 5th edition ○ Author: Diane Zak 54