|
-
Jul 20th, 2010, 02:46 AM
#1
Thread Starter
New Member
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
-
Jul 20th, 2010, 04:39 AM
#2
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.
-
Jul 20th, 2010, 08:35 AM
#3
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
-
Jul 21st, 2010, 02:06 AM
#4
Thread Starter
New Member
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!
-
Jul 22nd, 2010, 09:46 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|