New to VB, need help with comparing non-numerical and numerical data.

I am doing my first homework assignment for my Visual Basic college class, so I'm pretty new with this language. I'm pretty familiar with programming and structure in general, so don't feel like you have to talk down to me. =]

Ok, here's my problem (i searched the help for at least an hour looking for this answer, help would be appreciated): I'm making a simple program that asks for the radius of a circle and calculates area, circumference, etc. The problem occurs when I enter a non-numerical value, the program can't multiply 2 by a space or the letter z.

Can anybody tell me a way to check if the string is a non-numerical value and save the result as True or False? Any other suggestions would help, too. Thanks!

Module Module1

Sub Main()

Dim close, radiusAsString

close ="n"

While close <>"Y"

Console.Out.WriteLine()

Console.Out.WriteLine("What is the radius?")

radius = Console.In.ReadLine()

Console.Out.WriteLine("The Diameter is: " & 2 * radius)

Console.Out.WriteLine("The Area of the circle is: " & radius * radius * Math.PI)

Console.Out.WriteLine("The Circumference of the circle is: " & 2 * Math.PI * radius)

Console.Out.WriteLine()

Console.Out.WriteLine("Close? (Y/N)")

close = Console.In.ReadLine()

If close ="y"Then

close ="Y"

EndIf

EndWhile

EndSub

EndModule

[2931 byte] By [tehbagel] at [2007-12-24]
# 1

use Char.IsDigit(string) to check if the character entered is a numerical/digit. Example:

if Char.IsDigit(inputString) = false then

Console.WriteLine("Incorrect value entered")

end if

does this help?

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
Use the built-in IsNumeric function:

If Not IsNumeric(radius) Then
MsgBox("Please enter a valid number", MsgBoxStyle.Exclamation, "Nobugz was here")
Else
'... Rest of your code
End If

Your code will be much cleaner, and compile properly with Option Strict On, if you declare radius As Double. For example:

Option Strict On
Module Module1

Sub Main()
Dim radius As Double = 0
Console.Write("What is the radius? ")
If Not Double.TryParse(Console.ReadLine, radius) Then
MsgBox("Please enter a valid number", MsgBoxStyle.Exclamation, "Nobugz likez this")
Else
'... Rest of your code here
End If
End Sub

End Module

Make it a habit to put Option Strict On at the top of your source code. You'll be mystified for about a month but understand data types much better after that. And get extra credit from your prof. And fame and fortune in the future. To turn it on permanently, use Tools + Options, Projects and Solutions, VB defaults. Turn Option Explicit on too.

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Thank you both! Just what I needed.

Thanks again!

tehbagel at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...