What am I missing?

jdjd1118

Member
Joined
Nov 30, 2005
Messages
12
Programming Experience
Beginner
I am working on a program for fun, that when you enter a person's age the program can tell if it is within this century. Simply put, a person enters an age and the computer subtracts the current year from the age to see if the age is positive. I've run into a weird problem though. Most of the time, the program sees the given age as being greater than 2008. A sample is listed below:

Dim age As String = TextBox1.Text

Dim year As String = Date.Now.Year

If age > year Then
MsgBox("Greater than")

End If

It doesn't always show the msgbox, but try entering in 23 and it will but enter 20 and it does not. Thanks for any help!
 
That's because both age and year are strings and the '>' is doing text comparison not numeric comparison. You want something like this:
VB.NET:
Dim Age As Integer = CInt(Textbox1.Text)
If Age > Date.Now.Year Then Messagebox.Show("Greater than")
For something like this, you could use a NumericUpDown control which'll help make comparisons like this less likely to error (user input)
 
Back
Top