|
-
Dec 4th, 2009, 05:17 AM
#1
Thread Starter
Registered User
date validation
im trying to get the user of my program to enter their date of birth ad for the program to return how old they are in years, months and days (you are 10 years, 3 months and 23 days old) type thing and if its their birthday, to return an appropriate message.
atm ive got a textbox for the days, a combo box for the month and another textbox for the year. i guess the real question is how do i use these to do the above. im very new to programming and this is my first project outside of tutorials ive done so please explain everything as clearly as you can.
(please if you could give me little bits of code to help PLEASE )
thanks in advance
-
Dec 4th, 2009, 05:39 AM
#2
Addicted Member
Re: date validation
Try this snippet 
Code:
Module Module1
Enum DateTypes
Month = 0
Day = 1
Year = 2
End Enum
Sub Main()
Dim DateNow(2), BirthDate(2), DaysOld, MonthsOld, YearsOld As Int32
Dim sArr() As String = DateString.Split("-")
For i As Int32 = 0 To 2
DateNow(i) = sArr(i)
Next
Console.Write("Enter birthdate(XX-XX-XXXX) : ")
sArr = Console.ReadLine.Split("-")
For i As Int32 = 0 To 2
BirthDate(i) = sArr(i)
Next
YearsOld = DateNow(DateTypes.Year) - BirthDate(DateTypes.Year)
MonthsOld = DateNow(DateTypes.Month) - BirthDate(DateTypes.Month)
DaysOld = DateNow(DateTypes.Day) - BirthDate(DateTypes.Day)
If (DaysOld < 0) Then
DaysOld = DaysInMonth(DateNow(DateTypes.Month)) + DaysOld
MonthsOld -= 1
End If
If (MonthsOld < 0) Then
MonthsOld = 12 + MonthsOld
YearsOld -= 1
End If
Console.WriteLine("You are " & YearsOld & " years, " & MonthsOld & " months, and " & DaysOld & " days old.")
Console.ReadKey()
End Sub
Function DaysInMonth(ByVal MonthID As Int32) As Int32
Dim MonthArray() As Int32 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
Return MonthArray(MonthID - 1)
End Function
End Module
EDIT
Sorry about the clear explanation missing, but I'm running late already
-
Dec 4th, 2009, 05:45 AM
#3
Re: date validation
Since you are using textboxes, It's highly recommended you validate the values inside the textbox before you even attempt forming a date. This is so that if a user inputs irrational data (not a number between 1 and 31, for instance,) you can block execution and make the user fix their input.
Code:
Dim intDay as Integer
If Not Integer.TryParse(TextDay.Text, intDay) Then
Messagebox.Show("The day you have entered is invalid! Please fix it!")
Return
End If
'intDay will now contain a number
If IntDay < 1 Or IntDay > DateTime.DaysInMonth(SelectedYear, SelectedMonth)
Messagebox.Show("The day you have entered is invalid! Please fix it!")
Return
End if
This code is just a tip of the iceburg. Just copying and pasting will not make it work. Also, with some editing, this code will work for the year.
From my burrow, 2 feet under.
-
Dec 4th, 2009, 05:45 AM
#4
Thread Starter
Registered User
Re: date validation
thanks for that but im sorry but i dont understand what most of it is doing, and it doesnt appear to be working
(my form has tbDays, cbMonths, tbYears and Button1)- where do i put these in, its probably that- my fault
sorry- this is for the big one at the top
Last edited by stuart1512; Dec 4th, 2009 at 05:50 AM.
-
Dec 4th, 2009, 06:02 AM
#5
Addicted Member
Re: date validation
Copy & Paste this into your code
Code:
Function GetTimeLived(ByVal Days As Int32, ByVal Months As Int32, ByVal Years As Int32) As String
Dim DateNow(2), BirthDate(2), DaysOld, MonthsOld, YearsOld As Int32
Dim sArr() As String = DateString.Split("-")
For i As Int32 = 0 To 2
DateNow(i) = sArr(i)
Next
BirthDate(0) = Months
BirthDate(1) = Days
BirthDate(2) = Years
YearsOld = DateNow(DateTypes.Year) - BirthDate(DateTypes.Year)
MonthsOld = DateNow(DateTypes.Month) - BirthDate(DateTypes.Month)
DaysOld = DateNow(DateTypes.Day) - BirthDate(DateTypes.Day)
If (DaysOld < 0) Then
Dim MonthArray() As Int32 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
Dim DaysInMonth = MonthArray(DateNow(DateTypes.Month) - 1)
DaysOld = DaysInMonth + DaysOld
MonthsOld -= 1
End If
If (MonthsOld < 0) Then
MonthsOld = 12 + MonthsOld
YearsOld -= 1
End If
Return ("You are " & YearsOld & " years, " & MonthsOld & " months, and " & DaysOld & " days old.")
End Function
And call it(for example) like this:
Code:
MsgBox(GetTimeLived(05, 07, 1988))
-
Dec 4th, 2009, 06:25 AM
#6
Thread Starter
Registered User
Re: date validation
im not trying to be a pain but that doesnt seem to be working either- ive done exactly what you said, ive put the big bit at the top but outside the button click then inside the button click the message box call but changed it to instead of the numbers ive put my textboxes and the combobox
-
Dec 4th, 2009, 06:40 AM
#7
Addicted Member
Re: date validation
 Originally Posted by stuart1512
im not trying to be a pain but that doesnt seem to be working either- ive done exactly what you said, ive put the big bit at the top but outside the button click then inside the button click the message box call but changed it to instead of the numbers ive put my textboxes and the combobox
Exactly what part of it doesn't work? Are you getting errors? If so, what kind? What about posting your whole forms code?
-
Dec 4th, 2009, 06:54 AM
#8
Thread Starter
Registered User
Re: date validation
this is the code ive got along with the errors at the bottom
(i have tbDays for the days, cbMonths for the months, tbYears for the Y
years and a button- Button1)
Code:
Public Class Form1
Function
Dim GetTimeLived(ByVal Days As Int32, ByVal Months As Int32, ByVal Years As Int32) As String
Dim DateNow(2), BirthDate(2), DaysOld, MonthsOld, YearsOld As Int32
Dim sArr() As String = DateString.Split("-")
For i As Int32 = 0 To 2
DateNow(i) = sArr(i)
Next
BirthDate(0) = Months
BirthDate(1) = Days
BirthDate(2) = Years
YearsOld = DateNow(DateTypes.Year) - BirthDate(DateTypes.Year)
MonthsOld = DateNow(DateTypes.Month) - BirthDate(DateTypes.Month)
DaysOld = DateNow(DateTypes.Day) - BirthDate(DateTypes.Day)
If (DaysOld < 0) Then
Dim MonthArray() As Int32 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
Dim DaysInMonth = MonthArray(DateNow(DateTypes.Month) - 1)
DaysOld = DaysInMonth + DaysOld
MonthsOld -= 1
End If
If (MonthsOld < 0) Then
MonthsOld = 12 + MonthsOld
YearsOld -= 1
End If
Return ("You are " & YearsOld & " years, " & MonthsOld & " months, and " & DaysOld & " days old.")
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim GetTimeLived As String
MsgBox(GetTimeLived(tbDays, cbMonths, tbYears))
End Sub
End Class
Error 1 Identifier expected. C:\Documents and Settings\Stuart\My Documents\Trinity\VBTutorials\TEST PROJECTS\DateValidation\DateValidation\Form1.vb 3 12 DateValidation
Error 2 Value of type 'System.Windows.Forms.TextBox' cannot be converted to 'Integer'. C:\Documents and Settings\Stuart\My Documents\Trinity\VBTutorials\TEST PROJECTS\DateValidation\DateValidation\Form1.vb 38 29 DateValidation
Error 3 Too many arguments to 'Public ReadOnly Default Property Chars(index As Integer) As Char'. C:\Documents and Settings\Stuart\My Documents\Trinity\VBTutorials\TEST PROJECTS\DateValidation\DateValidation\Form1.vb 38 37 DateValidation
-
Dec 4th, 2009, 09:03 AM
#9
Addicted Member
Re: date validation
Here's it fixed
Code:
Public Class Form1
Function GetTimeLived(ByVal Days As Int32, ByVal Months As Int32, ByVal Years As Int32) As String
Dim DateNow(2), BirthDate(2), DaysOld, MonthsOld, YearsOld As Int32
Dim sArr() As String = DateString.Split("-")
For i As Int32 = 0 To 2
DateNow(i) = sArr(i)
Next
BirthDate(0) = Months
BirthDate(1) = Days
BirthDate(2) = Years
YearsOld = DateNow(DateTypes.Year) - BirthDate(DateTypes.Year)
MonthsOld = DateNow(DateTypes.Month) - BirthDate(DateTypes.Month)
DaysOld = DateNow(DateTypes.Day) - BirthDate(DateTypes.Day)
If (DaysOld < 0) Then
Dim MonthArray() As Int32 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
Dim DaysInMonth = MonthArray(DateNow(DateTypes.Month) - 1)
DaysOld = DaysInMonth + DaysOld
MonthsOld -= 1
End If
If (MonthsOld < 0) Then
MonthsOld = 12 + MonthsOld
YearsOld -= 1
End If
Return ("You are " & YearsOld & " years, " & MonthsOld & " months, and " & DaysOld & " days old.")
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(GetTimeLived(tbDays.Text, cbMonths.Text, tbYears.Text))
End Sub
End Class
You might want to check some basic tutorials about using functions and procedures, they're easy to learn and are the core of programming in general.
-
Dec 8th, 2009, 05:04 AM
#10
Thread Starter
Registered User
Re: date validation
thanks for that but where the function is called in the message box, there is a run time error
-when casting from a number, the value must be less than infinity
-make sure the source type is convertible to the destination type
-get general help for this exception
-InnerException: make sure your method arguments in the right format
-InnerException: when convertign a string to DateTime, parse the string to take the date before putting each variable into the DateTime object
-get general help for the InnerException
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|