CSCI1413 Programming - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

CSCI1413 Programming

Description:

What about those 'bigger and better idiots' ... only accepts numeric data what is to stop some idiot typing their name into it? ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 42
Provided by: cseD1
Category:

less

Transcript and Presenter's Notes

Title: CSCI1413 Programming


1
CSCI1413 Programming
  • Week 5
  • Topic Selection

2
Review of Last Week
  • Last week you were introduced to a range of data
    types and will have seen how data needs to be
    applied correctly to the correct variable types.
    You will also have designed test data for your
    programs.

3
Overview of This Week
  • Lab 1
  • You will be introduced to the process of
    validating user input. In learning about
    validation you will be introduced to an important
    programming concept called selection.

Lab 2 You will be introduced to the debugging
environment and see how it may be used to locate
errors.
4
Preview of Next Week
  • Next week you will be given a chance to apply
    everything you have learnt so far and create a
    program from scratch. Both lab sessions will be
    dedicated to this mini project.
  • Reading for this week Tutorial 8

5
Lab 1 Tutors Talk Validation
  • "Programming today is a race between software
    engineers striving to build bigger and better
    idiot-proof programs and the Universe trying to
    produce bigger and better idiots. So far, the
    Universe is winning."
  • Rich Cook

6
  • In last weeks work, you will have declared a set
    of variables to accept data from a
  • pre-defined form.
  • You should have a section of code that looks
    something like this

7
  • Dim FirstName As String
  • Dim SecondName As String
  • Dim Address As String
  • Dim PostCode As String
  • Dim TelephoneNo As String
  • Dim InvoiceNo As Integer
  • Dim InvoiceTotal As Single
  • Dim PaymentDate As Date
  • Dim Cash As Single
  • Dim Cheque As Single
  • Dim CreditCard As Single
  • Dim CreditCardNo As String
  • Dim Age As Integer

8
  • FirstName txtFirstName.Text
  • SecondName txtSecondName.Text
  • Age txtAge.Text
  • Address txtAddress.Text
  • PostCode txtPostCode.Text
  • TelephoneNo txtTelNo.Text
  • InvoiceNo txtInvoiceNo.Text
  • InvoiceTotal txtInvoiceTotal.Text
  • PaymentDate txtPaymentDate.Text
  • Cash txtCash.Text
  • Cheque txtCheque.Text
  • CreditCard txtCreditCard.Text
  • CreditCardNo txtCardNo.Text

9
  • You should have seen how selecting an incorrect
    data type for a variable will result in your
    program crashing.
  • For example
  • Dim FirstName as Integer
  • FirstNameFred
  • Would result in a crash because the Integer data
    type only accepts whole numeric values within a
    certain range.
  • Fred is textual data and inappropriate for the
    Integer data type. For FirstName we should be
    using the String data type.

10
  • So it is important that you select the right data
    type for your variables, however
  • What about those bigger and better idiots? What
    about the users that will be using the software
    you create?
  • As you saw when you created your test data,
    entering data of the wrong type will crash your
    program.

11
  • If I have a field on my form called Age which
    only accepts numeric data what is to stop some
    idiot typing their name into it? What if instead
    of entering 14 they type fourteen?
  • Sending the wrong data to the correct data type
    will end in the same result as selecting the
    wrong data type in the first place.
  • In order to stop users entering inappropriate
    data, we need some system of checking the data
    entered.

12
  • The process of checking data as it is entered is
    called validation.
  • In looking at validation we shall introduce some
    of the validation functions but more importantly
    we shall introduce the idea of selection.
  • Selection is all about making decisions.

13
  • we make decisions all of the time
  • If its last orders then buy another round.
  • If its raining then take an umbrella.
  • If it is 930am then think about going to the 9am
    lecture.
  • And so on.

14
  • Selection means we are making decisions based on
    certain criteria that may or may not be the case.
  • If your wallet is empty then you go to the cash
    point.
  • The decision is do we go to the cash point or
    not.
  • The criteria is how much money we may or may not
    have in our wallet.
  • In writing programs the ability to make decisions
    is vital for any programming language.

15
The IF Statement
  • The main statement used in VB for making
    decisions depending on different conditions is
    called IF.
  • We have seen how we can allocate RAM as variables
    to store data.
  • We have seen how we can perform processing on the
    contents of these variables and place the results
    into other variables.

16
  • The next step is to perform certain calculations,
    only if a specific variable contains a certain
    value.
  • For example if the time on the clock has reached
    zero, we detonate the bomb.
  • If the washing machine control has reached a
    certain point on the dial, we make the drum spin
    to clear water out of the clothes.

17
  • The If statement takes the following form
  • If condition Then
  • execute the code here
  • End If
  • In the above example, if the condition is true,
    then the code between If End If will run,
    otherwise, the code will be ignored.

18
In the following code
  • Dim AreaCode as String
  • Dim City as String
  • AreaCode0116
  • If AreaCode 0115 Then
  • City Nottingham
  • End If
  • If AreaCode 0116 Then
  • City Leicester
  • End If
  • The value of the variable City would be set to
    Leicester.

19
  • Since the value of AreaCode is 0116, only the
    line of code
  • City Leicester
  • will run,
  • not the code
  • City Nottingham
  • The If statements may be used to control which
    code runs under what set of circumstances.

20
However
  • Dim AreaCode as String
  • Dim City as String
  • AreaCode0117
  • City Unknown Area Code
  • If AreaCode 0115 Then
  • City Nottingham
  • End If
  • If AreaCode 0116 Then
  • City Leicester
  • End If
  • The value of the variable City would undefined

21
In the following better code
  • Dim AreaCode as String
  • Dim City as String
  • AreaCode0117
  • City Unknown Area Code
  • If AreaCode 0115 Then
  • City Nottingham
  • End If
  • If AreaCode 0116 Then
  • City Leicester
  • End If
  • The value of the variable City would be set to
  • Unknown Area Code

22
Using If for Validation.
  • Among many other things, If is helpful for
    validation.
  • We can use If to look at our data and decide if
    the data is useful/correct or not.
  • Assume we have a field to capture a persons age
    on a form.

23
  • How can we check to see if the user has typed a
    suitable age value or not?
  • We want the If statement to do something like
  • If the value entered by the user is a number Then
  • Assign the data to our variable
  • End If

24
The Boolean Data Type.
  • Before we do any more on If, we need to look at a
    new data type called Boolean.
  • Like all other data types, the Boolean data type
    is limited in its range and the data it is able
    to store.
  • The Boolean data type may only contain one of two
    values True or False

25
  • To declare a Boolean variable we might type
  • Dim IsItOK as Boolean
  • declares a new Boolean variable called IsItOk.
  • To assign the value True to it we might type
  • IsItOK True

26
Introduction to Validation Functions IsNumeric
  • VB provides a large range of functions that may
    be used for validation.
  • What these functions do is that they examine data
    and report back something about the state of the
    data.
  • One such function is called IsNumeric.
  • IsNumeric (as the name suggests) looks at data
    and reports back if it is numeric or not.
  • IsNumeric returns a Boolean value as a result of
    its test.
  • We can assign this Boolean value to a variable
    and use it in an If statement.

27
Bringing it all together.
  • Consider the following situation.
  • We have a form with an age field on it. we want
    the user to enter a valid numeric value, not
    text. If we try to process a non numeric value
    our program is going to crash.
  • We want the user to enter 30 not thirty.
  • The text box on the form is called txtAge.
  • We could first declare a variable to store the
    result of our test
  • Dim IsItOK as Boolean

28
  • We could then use IsNumeric to examine what the
    user typed and see if the value is any use
  • IsItOk IsNumeric(txtAge.Text)
  • In the above line, IsNumeric will examine the
    contents of the text box and assign a True or
    False value to the IsItOk variable depending on
    if the value in txtAge is a number or not.

29
  • Now the IsItOk variable may be used in an If
    statement to see if it is ok to place the value
    into a suitable variable
  • If IsItOk True then
  • Age txtAge.Text
  • End If

30
  • The whole section of code would read as follows
  • Dim IsItOK as Boolean
  • Dim Age as Integer
  • IsItOk IsNumeric(txtAge.Text)
  • If IsItOk True then
  • Age txtAge.Text
  • End If
  • Using the above example, should the user type
    text into the txtAge text box, IsNumeric will
    return False when it looks at the data and the
    value will not be placed into the Integer
    variable Age.

31
  • Validation is all well and good so long as the
    program doesnt crash, but it would be nice if we
    gave the user some idea of what they had done
    wrong.
  • Take the age example, if the user types thirty
    rather than 30. It would be nice if the user
    received an error message telling them what they
    had done wrong.
  • e.g.

32
(No Transcript)
33
If Then Else
  • To do this we can extend the If statement to
    include an Else option.
  • If the value entered is valid Then
  • assign the value to our variable
  • Else
  • display an error message
  • End If

34
  • The code for this would look like this
  • Dim IsItOK as Boolean
  • Dim Age as Integer
  • IsItOk IsNumeric(txtAge.Text)
  • If IsItOk True then
  • Age txtAge.Text
  • Else
  • MessageBox.Show("Age is not valid")
  • End If

35
(No Transcript)
36
Exercise
  • In this exercise you will extend the application
    used to illustrate various data types by adding
    suitable validation.
  • Copy the source code for this exercise from
  • L\mdean\csci1413\samples\validation
  • Open the project and open the code for the click
    event of the OK button.

37
  • The source code has been provided including
  • Dim FirstName As String
  • Dim SecondName As String
  • Dim InvoiceNo As Integer
  • Dim InvoiceTotal As Single
  • Dim PaymentDate As Date
  • FirstName txtFirstName.Text
  • SecondName txtSecondName.Text
  • InvoiceNo txtInvoiceNo.Text
  • InvoiceTotal txtInvoiceTotal.Text
  • PaymentDate txtPaymentDate.Text

38
  • to which you will add suitable validation using
    the correct validation functions and suitably
    placed If statements.
  • To validate numbers you need to use IsNumeric.
  • To validate dates you need to use IsDate.
  • Notice that the lines of code that do the
    assignment have been commented out. (The lines in
    green!)
  • This has been done to help you not get confused.

39
  • You will find it simpler if you write the code
    processing each field one at a time. i.e. write
    the code to process the First Name, get that
    working and tested, then do Second Name, then do
    Age.
  • So to activate the line of code that assigns the
    First Name field to the FirstName variable remove
    the comment symbol .
  • If you do too much at once you may have more
    errors to fix and you might get confused.

40
Try to solve one error at a time!
  • When you think that a field has been suitably
    validated, try entering your valid / invalid test
    data from last weeks exercise to see if your
    program crashes or not.

41
And now
  • Its over to you
Write a Comment
User Comments (0)
About PowerShow.com