2. 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
3. Sometimes You want the user to
enter only numbers . . .
If I enter the number 5, the program works
4. Sometimes You want the user to
enter only numbers . . .
If I enter five however, things start to break down
5. Sometimes You want the user to
enter only numbers . . .
BUT THAT
STILL MAKES
SENSE TO
ME!!
6. Sometimes You want the user to
enter only numbers . . .
Visual Studio is telling us there’s a InvalidCastException
7. 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 five
into the variable favouriteNumber
8. Remember, Variables have a specific
data type
Variables have a
I’m the type of data type
Variable that
holds only
Champagne !
Don’t you try
and put
Lemonade in
me!
Champagne
Bucket
9. Example data types
String
Integer
Double
Float
Champagne Char
Bucket
Boolean
13. isNumeric
We can use the isNumeric() function to tell if a
number entered by the user is a number
IsNumeric(temp)
The variable it will
check is a number
15. Adding isNumeric
Module 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