Is numeric

Post on 01-Jul-2015

179 views 6 download

Transcript of Is numeric

IS NUMERIC?Just one way we can protect the user

from themselves!

Sometimes You want the user to enter only numbers . . .

Module Module1

Sub Main()

Dim favouriteNumber As Integer

Console.WriteLine("What's your favourite number?")

favouriteNumber = Console.ReadLine()

Console.WriteLine("Your favourite number is " &

favouriteNumber)

Console.ReadLine()

End Sub

End Module

Sometimes You want the user to enter only numbers . . .

If I enter the number 5, the program works

Sometimes You want the user to enter only numbers . . .

If I enter five however, things start to break down

Sometimes You want the user to enter only numbers . . .

BUT THAT STILL MAKES

SENSE TO ME!!

Sometimes You want the user to enter only numbers . . .

Visual Studio is telling us there’s a InvalidCastException

Sometimes You want the user to enter only numbers . . .

Visual Studio is telling us there’s a InvalidCastException

Visual Studio is telling us that it can’t put the word fiveinto the variable favouriteNumber

Remember, Variables have a specific data type

ChampagneBucket

Variables have a data typeI’m the type of

Variable that holds only

Champagne ! Don’t you try

and put Lemonade in

me!

Example data types

ChampagneBucket

String IntegerDouble

Float Char

Boolean

You need to protect the user

from themselves . . .

You’re gonna need some isNumeric

validationprotection . . .

!

isNumeric

IsNumeric(temp)

We can use the isNumeric() function to tell if a number entered by the user is a number

The variable it will check is a number

isNumericisNumeric() will return either true or false depending

on the input it is given

Adding isNumericModule Module1

Sub Main()

Dim favouriteNumber As Integer

Console.WriteLine("What's your favourite number?")

Dim temp As String

temp = Console.ReadLine()

While Not IsNumeric(temp)

Console.WriteLine("YOU HAVEN'T ENTERED NUMBER!")

temp = Console.ReadLine()

End While

favouriteNumber = temp

Console.WriteLine("Your favourite number is " & favouriteNumber)

Console.ReadLine()

End Sub

End Module

Adding isNumeric

The user has been stopped from entering the word five

Thanks!