Results 1 to 8 of 8

Thread: Could this be done in a better way?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    4

    Could this be done in a better way?

    Hi everybody!

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

    Code:
    ' 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

  2. #2
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: Could this be done in a better way?

    Yep!!

    Code:
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    4

    Re: Could this be done in a better way?

    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

  4. #4
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: Could this be done in a better way?

    What version of VB are you using?

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    4

    Re: Could this be done in a better way?

    Uhm, the latest? I'm using Visual Studio 2005 to code anyway.

  6. #6
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: Could this be done in a better way?

    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    4

    Re: Could this be done in a better way?

    I took your code snippet and made it into VB.NET.
    Here's the result:
    Code:
            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!

  8. #8
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Could this be done in a better way?

    vb Code:
    1. Private Sub DateParser(ByVal input As String)
    2.  
    3.     Dim dt As Date
    4.  
    5.     If Date.TryParseExact(input, "MMddyy", Nothing, Globalization.DateTimeStyles.None, dt) Then
    6.         Dim args() As Object = New Object() {Environment.NewLine, dt.ToShortDateString, dt.Year, dt.Month, dt.Day}
    7.         Dim msg As String = String.Format("Your date: {1}{0}Year: {2}{0}Month: {3}{0}Day: {4}", args)
    8.         MessageBox.Show(msg, "Result", MessageBoxButtons.OK)
    9.     Else
    10.         MessageBox.Show("Invalid date specified.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    11.     End If
    12.  
    13. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width