17
IS NUMERIC? Just one way we can protect the user from themselves!

Is numeric

Embed Size (px)

Citation preview

Page 1: Is numeric

IS NUMERIC?Just one way we can protect the user

from themselves!

Page 2: Is numeric

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

Page 3: Is numeric

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

If I enter the number 5, the program works

Page 4: Is numeric

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

If I enter five however, things start to break down

Page 5: Is numeric

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

BUT THAT STILL MAKES

SENSE TO ME!!

Page 6: Is numeric

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

Visual Studio is telling us there’s a InvalidCastException

Page 7: Is numeric

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

Page 8: Is numeric

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!

Page 9: Is numeric

Example data types

ChampagneBucket

String IntegerDouble

Float Char

Boolean

Page 10: Is numeric

You need to protect the user

from themselves . . .

Page 11: Is numeric

You’re gonna need some isNumeric

validationprotection . . .

Page 12: Is numeric

!

Page 13: Is numeric

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

Page 14: Is numeric

isNumericisNumeric() will return either true or false depending

on the input it is given

Page 15: Is numeric

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

Page 16: Is numeric

Adding isNumeric

The user has been stopped from entering the word five

Page 17: Is numeric

Thanks!