CW-V1 SDD 0601 Principals of Software Design and Development Variables Constants Data Types Error Handling Starter: BlockbustersBlockbusters
CW-V1 SDD 0602 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 0603 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 0604 Declaring Variables We declare variables to tell the program: Their Name What they will hold Dim Glass as Beer Glass = “Full” Declaration Keyword NameData type Initialise the variable Initial value: Full
CW-V1 SDD 0605 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 0606 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 0607 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 0608 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 0609 Quiz Declare and Initialise variables for the following scenarios A telephone number – 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 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 Quiz TelNo = “ ” Course = “A Level English” Age = “Integer” DateOfBirth = “01/02/1989” BusFare = 3.50 DinnerTickets = True Student Name = “Tom Brown”
CW-V1 SDD 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 Calculations Use variables just like numbers! Dim FirstNo as Integer Dim SecondNo as Integer Dim Result as Integer FirstNo = 2 SecondNo = 5 FirstNo + SecondNo = 7 SecondNo – FirstNo = 3
CW-V1 SDD Comparisons Operators are used to compare two values x = yx is exactly the same as y OR make x the same as y x > yx is bigger than y x < yx is smaller than y x >= yx is bigger than or equal to y x <= yx is smaller than or equal to y x <> yx is not the same as y
CW-V1 SDD 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 Quick Quiz PhoneBill > GasBill Pink <> Green ElectricBill <= GasBill Starburst = OpalFruits
CW-V1 SDD Option Explicit Your 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 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 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 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