![Page 1: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/1.jpg)
Variables in VB
![Page 2: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/2.jpg)
What is a variable?►A named memory location that stores
a value
![Page 3: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/3.jpg)
Why use variables?►Makes code easier to read►Used to transfer information from one
part of a program to another
![Page 4: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/4.jpg)
Types of VariablesTYPE PREFIXSingle sglDouble dblInteger intLong lngCurrency curString strBoolean bln
![Page 5: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/5.jpg)
Single►Positive or negative real (might
contain a decimal) numbers►Can store smaller numbers
![Page 6: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/6.jpg)
Double►Same as single, but can store larger
numbers
![Page 7: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/7.jpg)
Integer►Positive or negative integers (no
decimals)►Smaller integers►Good for counting (e.g. number of
students in a class)►NOTE: if a decimal number is assigned
to an integer, then the value is rounded
►E.g. 6.7 would become 7
![Page 8: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/8.jpg)
Long►Same as integer, but can store larger
numbers
![Page 9: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/9.jpg)
Currency►Real numbers that are money values►Up to four decimal places
![Page 10: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/10.jpg)
String►Set of characters (e.g. letters or any
character that can be typed)►String assignments require quotation
marks “” ►E.g. strword = “hello”
![Page 11: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/11.jpg)
Boolean►True or false►Used for on/off and yes/no values
![Page 12: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/12.jpg)
Using VariablesStep 1 – DeclareStep 2 – Initialize (Assign a value) Step 3 – Use
![Page 13: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/13.jpg)
Declaring VariablesE.g.
Dim intage As Integer
►Dim - stands for dimension (a variable has been assigned a space in memory somewhere)
►intage – identifier (name of variable)►Integer – data type
![Page 14: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/14.jpg)
Declaring VariablesWhen a variable has been declared, you have
saved a space in memory to hold whatever value is assigned:
intage
![Page 15: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/15.jpg)
Initializing Variables (assigning a value)
E.g.intage = 15 OR
intage = txtage.text
This will assign whatever the user types into the textbox to intage.
![Page 16: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/16.jpg)
Initializing Variables► The space in memory now holds a value
15
intage
![Page 17: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/17.jpg)
Using the VariableE.g.intagedoubled = intage * 2
This will calculate the person’s age multiplied by 2 and then will store it in the variable called: intagedoubled.
You can then output the variable intagedoubled to a textbox.
txtagedoubled.text = intagedoubled
![Page 18: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/18.jpg)
Questions1 – Write a variable declaration
statement for:a) A variable called intgrade of integer
type
b) A variable called strword of string type
![Page 19: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/19.jpg)
Questions2 – Indicate the value of the following
variables:a) Dim dblnumber As Double
dblnumber = 6 – 3
dblnumber
![Page 20: Variables in VB. What is a variable? ► A named memory location that stores a value](https://reader038.vdocuments.mx/reader038/viewer/2022100505/5a4d1b627f8b9ab0599aded0/html5/thumbnails/20.jpg)
Questions2 – b) Dim intnum1 As Integer
Dim intnum2 As Integer Dim intnum3 As Integer
intnum1 = 3intnum2 = 4intnum3 = intnum1 * intnum2
intnum1 intnum2 intnum3