Validation and Selection. - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Validation and Selection.

Description:

In order to stop users entering inappropriate ... E.g. If your wallet is empty then you go to the cash point. The decision - do we go to the cash point or not. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 14
Provided by: indi
Category:

less

Transcript and Presenter's Notes

Title: Validation and Selection.


1
Validation and Selection.
  • "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

2
Poorly Handled Data Causes Crashes!
  • Dim FirstName as Integer
  • FirstNameFred
  • Or
  • A field called Age ,
  • What if instead of entering 14 you type fourteen?

3
  • In order to stop users entering inappropriate
    data, we need some system of checking the data
    entered.
  • 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.

4
Selection
  • Selection is all about making decisions.
  • 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.

5
Decisions and Criteria
  • Selection means making decisions based on certain
    conditions.
  • E.g. If your wallet is empty then you go to the
    cash point.
  • The decision - do we go to the cash point or
    not.
  • The condition - how much money we may or may not
    have in our wallet.

6
The IF Statement
  • To code decisions in VB we use the If statement.
  • 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.

7
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
  • Q. What would be the value of the variable City?

8
Using If for Validation.
  • 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
  • Use the data
  • End If

9
Boolean Data Types and Validation Functions.
  • Before looking any further at If Statements, we
    shall introduce two other programming topics
  • 1. The Boolean Data Type
  • 2. Validation Functions.

10
The Boolean Data Type.
  • May only contain one of two values
  • True or False
  • To declare a Boolean variable
  • Dim IsItOK as Boolean
  • declares a new Boolean variable called IsItOk.

11
Validation Functions 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.

12
Bringing it all together.
  • Dim IsItOK as Boolean
  • Dim Age as Integer
  • IsItOk IsNumeric(txtAge.Text)
  • If IsItOk True then
  • Age txtAge.Text
  • End If

13
If Then Else.
  • 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
Write a Comment
User Comments (0)
About PowerShow.com