21
CW-V1 SDD 060 1 Principals of Software Design and Development Variables Constants Data Types Error Handling Starter: Blockbusters

CW-V1 SDD 0601 Principals of Software Design and Development Variables Constants Data Types Error Handling Starter: BlockbustersBlockbusters

Embed Size (px)

Citation preview

CW-V1 SDD 060 1

Principals of Software Design and Development

VariablesConstantsData Types

Error HandlingStarter: Blockbusters

CW-V1 SDD 060 2

Learning Objectives

Describe Variables and Constants Further describe the Error handling Identify the need for good

documentation of the above Improve the Business Card Program!

CW-V1 SDD 060 3

Variables

A variable is a named piece of memory that holds information

Think of it like a glass that can hold a measured amount of something

The contents of the glass is called a data type

The amount of contents would be its value

CW-V1 SDD 060 4

Declaring Variables

We declare variables to tell the program: Their Name What they will hold

Dim Glass as Beer Glass = “Full”

Declaration Keyword

Name Data type

Initialise the variable

Initial value: Full

CW-V1 SDD 060 5

Declaring and Initialising Variables

Dim PupilName as String Dim PupilGrade as Integer

Name = “Hermione” Grade = 10

Variable names cannot contain

spaces. Begin each word with a capital

instead

CW-V1 SDD 060 6

Variables

Its value can be changed within the code

Variables can be used to do mathematics

It is better to use a variable than just type in a number Makes code easier to read Changes can be made more easily

CW-V1 SDD 060 7

Data Types

A variable can only be of one data type Some data types take up more memory

than others Always use the least amount of memory

possible to keep programs running efficiently

Some data types have specialist functions

CW-V1 SDD 060 8

The Money Trap

The Currency data type cannot be formatted to display only two decimal places – this is a problem with VB6

To display money properly you must use Single

Single variables may be formatted to display a set number of decimal places Dim Cost as Single Cost = 5.99 The pound sign

will be added later when the value is

displayed.

CW-V1 SDD 060 9

Quiz

Declare and Initialise variables for the following scenarios A telephone number – 511 6000 The name of a course – A Level English A student’s age – 17 Student’s date of birth – 01/02/1989 A bus fare - £3.50 Dinner tickets issued – Yes Student Name – Tom Brown

CW-V1 SDD 060 10

Quiz

Dim TelNo as String

Dim Course as String

Dim Age as Integer

Dim DateOfBirth as Date

Dim BusFare as Single

Dim DinnerTickets as Boolean

Dim StudentName as String

CW-V1 SDD 060 11

Quiz

TelNo = “511 6000”

Course = “A Level English”

Age = “Integer”

DateOfBirth = “01/02/1989”

BusFare = 3.50

DinnerTickets = True

Student Name = “Tom Brown”

CW-V1 SDD 060 12

Constants

Constants are like variables that cannot be changed once initialised

Use them for values that will not change while the program is running, e.g. VAT

Make things easier to remember e.g. the colour constant vbGreen replaces the value &H0080FF80& !!

Their names usually begin with “c” e.g. cMin

CW-V1 SDD 060 13

Calculations

Use variables just like numbers!

Dim FirstNo as IntegerDim SecondNo as IntegerDim Result as Integer

FirstNo = 2SecondNo = 5

FirstNo + SecondNo = 7SecondNo – FirstNo = 3

CW-V1 SDD 060 14

Comparisons Operators are used to compare two values

x = y x is exactly the same as y

OR

make x the same as y

x > y x is bigger than y

x < y x is smaller than y

x >= y x is bigger than or equal to y

x <= y x is smaller than or equal to y

x <> y x is not the same as y

CW-V1 SDD 060 15

Quick Quiz

Use comparison operators to write expressions for these scenarios Phone bill is higher than gas bill Pink is not equal to green Electric bill is the same or less than gas bill Starburst same as Opal Fruits

CW-V1 SDD 060 16

Quick Quiz

PhoneBill > GasBill Pink <> Green ElectricBill <= GasBill Starburst = OpalFruits

CW-V1 SDD 060 17

Option ExplicitYour New Best Friend

Who has had problems with spelling mistakes in code?

Put Option Explicit in the General Declarations of a form’s code Keywords, variable and constant names

are capitalised and corrected Errors are highlighted when you run

code

CW-V1 SDD 060 18

Error Handling – Why Bother?

Errors can make programs Crash, freeze or end unexpectedly Difficult to use Give inaccurate information

Try to think of errors likely to occur Test for errors Trap errors – tell the computer what to do if

they occur, e.g. display a message It is never possible to trap all errors!

CW-V1 SDD 060 20

Error Messages

Good error messages should Explain what the problem was

Use Plain English Give examples if necessary

Why it occurred How to fix it What the user should do next Give options as appropriate

CW-V1 SDD 060 21

More about MsgBox

Title

Style (icon)

Style (buttons)

Message

Put line breaks into your messages by using the constant vbctrlf

e.g. MsgBox “This will be the top line” & vbcrlf & “and this will be the bottom line”

Default choice

Message boxes can be customised to be more user friendly