U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

13
Using Variables in Visual Basic Intermediate 2 Software Development

Transcript of U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

Page 1: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

Using Variablesin Visual Basic

Intermediate 2Software Development

Page 2: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

Learning Objectives

Page 3: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

What is a variable

• Programs need to be able to store values, either entered by the user, or created as the result of a calculation

• A variable is a label given to an item of data so that program instructions can work with it.

Page 4: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

Data Types

• You learnt in the Computer Systems unit, that computers store different types of data in different ways.

• It is good programming practice to consider all the data that will need to be stored while the program is running.

Page 5: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

Data Types

• We will consider three types of data in this course. – Integers– Single– Strings

Page 6: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

Declaring Variables

• Visual BASIC needs to know what type of data it will be storing and processing in any program. To do this, we “declare variables” at the start of the program, using lines like:

• Dim no_in_class as Integer • Dim surname as String • Dim price as Single

Page 7: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

When the VB system “reads” these statements at the start of a program, it sets up a storage space of the appropriate type in the computer’s RAM, and labels it with the variable name given. It is useful to imagine these electronic storage spaces as labelled boxes in which data can be stored, like this …………..

Page 8: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

• An integer variable, called no_in_class, storing value 18.

no_in_class

18

Page 9: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

• A string variable, called name, storing value “Gavin”.

Name

Gavin

Page 10: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

• A single variable, called price ,storing value 2.99.

price

2.99

Page 11: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

Variables• These “storage boxes” are called variables

because the actual value of the data they store can vary or change during the running of a program.

• • It is important to make sure that all variables are

correctly declared – the right type (integer, string or single) – and with sensible, readable variable names.

Page 12: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

Naming Variables

Variables can have almost any name, but each variable name:•must begin with a letter•must not be a VB keyword (like End or Print or MsgBox)•must not contain spaces (no_of_pupils is OK, but no of pupils is not).•

Page 13: U SING V ARIABLES IN V ISUAL B ASIC Intermediate 2 Software Development.

Now do the worked example – pages 53 – 60 in the booklet – “Belinda’s Slab Calculator”