PDA

Click to See Complete Forum and Search --> : Could this be done in a better way?


wally_91
Oct 20th, 2007, 06:07 PM
Hi everybody!

I'm wondering if I can make this code snippet better, and in that case HOW?


' My task:
'Write a program that receives a six-digit date for example 071019
'The program is suppose to find out if the date is correct.
'Rules
'• April, June, September, November all have 30 days.
'• february has 28 days if the year is not a leap year: then the month has 29 days
'• all other months have 31 days
'• if the year is a leap year then the two last digits in the year are even: dividable with 4.

Dim d, m, y As String
Dim dedate As Boolean

If TextBox9.Text.Length > 0 And TextBox9.Text.Length = 6 Then
y = TextBox9.Text.Substring(0, 2)
m = TextBox9.Text.Substring(2, 2)
d = TextBox9.Text.Substring(4, 2)

'leap year?
y /= 4
If y = 0 Or y = 0.5 Or y = 1 Or y = 1.5 Or y = 2 Then
y = True
Else : y = False
End If
'y = false => not leap year; y = true => leap year

'are months between 1 and 12?
If m > 0 And m < 13 Then
'if month = apr, jun, sep or nov or d > 0 and d < 31
If (m = 4 Or m = 6 Or m = 9 Or m = 11) And d > 0 And d < 31 Then
'max days of teh month = 30
dedate = True
ElseIf m = 2 And d > 0 Then
'Only februari
If y = False And Not d > 28 Then
'february not leap year and days ok
dedate = True
ElseIf y = True And d < 30 Then
'february and leap year
dedeate = True
ElseIf (y = False And d > 28) Or d > 29 Then
dedate = False
End If
ElseIf m = 1 Or m = 3 Or m = 5 Or m = 6 Or m = 7 Or m = 8 Or m = 10 Or m = 12 And d > 0 And d < 32 Then
dedate= True
Else
dedate= False
End If

'MsgBox("Your date: " & datum & vbNewLine & "Year: " & y & vbNewLine & "Month: " & m & vbNewLine & "Day: " & d, , "Result")
Else
dedate= False
End If
Else
dedate= False
End If

Label26.Text = "The daet is " & dedate.ToString.ToLower & "."

Thx very much!

//wally_91

03myersd
Oct 21st, 2007, 03:37 AM
Yep!!

Private Sub Command1_Click()
Dim str As String
Dim date1 As Date

str = "071019"
str = Left(str, 2) & "/" & Mid(str, 3, 2) & "/" & Right(str, 2)
date1 = str
If IsDate(str) Then
Print "Date: " & date1
Print "Year: " & Year(date1)
Print "Month: " & Month(date1)
Print "Day: " & Day(date1)
End If
End Sub

wally_91
Oct 21st, 2007, 05:39 AM
Wow, that's really impressive!

Anyway, I get these error messages:
Error 1 'Public Property Left() As Integer' has no parameters and its return type cannot be indexed. C:\Documents and Settings\\Form1.vb 187 15 program
Error 2 'Public ReadOnly Property Right() As Integer' has no parameters and its return type cannot be indexed. C:\Documents and Settings\\Form1.vb 187 59 program
Error 3 'Day' is a type and cannot be used as an expression. C:\Documents and Settings\\Form1.vb 193 29 program

03myersd
Oct 21st, 2007, 06:30 AM
What version of VB are you using?

wally_91
Oct 21st, 2007, 06:59 AM
Uhm, the latest? I'm using Visual Studio 2005 to code anyway.

03myersd
Oct 21st, 2007, 07:05 AM
Thats the problem then... I coded before thinking. You need code for VB.NET

My code is for VB6. Someone else will need to help you. Sorry.

wally_91
Oct 25th, 2007, 09:49 AM
I took your code snippet and made it into VB.NET.
Here's the result:
Dim d, m, y, dd As String
Dim thedate As Boolean

If TextBox9.Text.Length = 6 And IsNumeric(TextBox9.Text) = True Then
y = TextBox9.Text.Substring(0, 2)
m = TextBox9.Text.Substring(2, 2)
d = TextBox9.Text.Substring(4, 2)
If d > 31 Or m > 12 Or d < 0 Or m < 0 Then
thedate = False
Else
dd = y & "/" & m & "/" & d
If IsDate(dd) = True Then
thedate = True
Else : thedate = False
End If
End If

'MsgBox("Your date: " & dd & vbNewLine & "Year: " & y & vbNewLine & "Month: " & m & vbNewLine & "Day: " & d, , "Result")
Else
MsgBox("You did something wrong :(", MsgBoxStyle.Critical, "Fel")
thedate = False
End If

Thanks for the inspiration!

wild_bill
Nov 5th, 2007, 10:56 AM
Private Sub DateParser(ByVal input As String)

Dim dt As Date

If Date.TryParseExact(input, "MMddyy", Nothing, Globalization.DateTimeStyles.None, dt) Then
Dim args() As Object = New Object() {Environment.NewLine, dt.ToShortDateString, dt.Year, dt.Month, dt.Day}
Dim msg As String = String.Format("Your date: {1}{0}Year: {2}{0}Month: {3}{0}Day: {4}", args)
MessageBox.Show(msg, "Result", MessageBoxButtons.OK)
Else
MessageBox.Show("Invalid date specified.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If

End Sub