variables in vb. what is a variable? ► a named memory location that stores a value

20

Click here to load reader

Upload: sabina-mcdaniel

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

Why use variables? ► Makes code easier to read ► Used to transfer information from one part of a program to another

TRANSCRIPT

Page 1: Variables in VB. What is a variable? ► A named memory location that stores a value

Variables in VB

Page 2: Variables in VB. What is a variable? ► A named memory location that stores a value

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Questions2 – b) Dim intnum1 As Integer

Dim intnum2 As Integer Dim intnum3 As Integer

intnum1 = 3intnum2 = 4intnum3 = intnum1 * intnum2

intnum1 intnum2 intnum3