Results 1 to 5 of 5

Thread: A coding question

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    2

    A coding question

    Hey guys. This is my first post here. I'm new to Visual Basic, just taking an introductory course that gets the basics out of the way. I just completed and turned in a project that left me with a concern. The assignment was to program Zeller's Congruence, basically an algorith that which, if you pass it a day of month, month, and year, it returns to you the day of the week that date occurred on.

    The assignment only called the algorithm to be correctly coded. However, I decided to go a little beyond the scope of the problem statement and put in some error checking. For instance, I wanted to make sure that when passing values in for the days of the month, only reasonable values would be placed (between 1-31 for most months, 1-30 for others, and of course 1-29 for February during leap years)

    Here's a snippet of my code (the entire thing is very long)
    Code:
    If MonthNumber = 13 Or MonthNumber = 3 Or MonthNumber = 5 Or MonthNumber = 7 Or MonthNumber = 8 Or MonthNumber = 10 Or MonthNumber = 12 Then
                            If DayValue <= 0 Or DayValue > 31 Then
                                MessageBox.Show("This month can only have between 1-31 days! Please re-enter day", "Data Entry Error",
                                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                With DayTextBox
                                    .Focus()
                                    .SelectAll()
                                End With
                            End If
                        End If
    So for this condition, I'm testing the months that end in 31 days and checking to see that if the user inputs a day value greater than 31, to return an error.

    My problem is this. Although I successfully return the error, the program will continue to send the values to the algorithm and calculate a value. Once the user hits the ok button on the message box, a day value is returned for a date that does not exist. How do I prevent the code from executnig once the error occurs. I don't want a value returned for input data that makes no sense.

    Thanks

  2. #2
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: A coding question

    You should use Exit Function or Exit Sub (probably function), to exit instead of continuing with the code.
    In your snippet
    Code:
    If MonthNumber = 13 Or MonthNumber = 3 Or MonthNumber = 5 Or MonthNumber = 7 Or MonthNumber = 8 Or MonthNumber = 10 Or MonthNumber = 12 Then
                            If DayValue <= 0 Or DayValue > 31 Then
                                MessageBox.Show("This month can only have between 1-31 days! Please re-enter day", "Data Entry Error",
                                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                With DayTextBox
                                    .Focus()
                                    .SelectAll()
                                End With
                                Exit Function
                            End If
                        End If
    Delete it. They just clutter threads anyway.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: A coding question

    Code:
        Private Function DayInWeek(ByVal aDate As String) As String
            Dim d As DateTime
            If DateTime.TryParse(aDate, d) Then
                'valid date time
                Return d.DayOfWeek.ToString 'return day of week
            Else
                'invalid
                Throw New Exception("Invalid Date")
            End If
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    2

    Re: A coding question

    Thank you very much guys!

    I had a feeling that it was a rather trivial question lol. But I swear, I could not find the answer for the life of me in the book, and online I guess I was just not typing the correct keywords to find an answer. I sort of feel foolish now for asking!

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: A coding question

    Familiarize yourself with what's offered by the API/framework so you don't end up trying to reinvent the wheel, e.g. manual check of values when you can just pass them on to DateTime.TryParse.

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