u sing v ariables in v isual b asic intermediate 2 software development

Post on 28-Dec-2015

227 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Using Variablesin Visual Basic

Intermediate 2Software Development

Learning Objectives

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.

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.

Data Types

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

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

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 …………..

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

no_in_class

18

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

Name

Gavin

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

price

2.99

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.

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).•

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

top related