(1) With an If: ...If intDay >= 0 AndAlso intDay <= 6 Then ......strName = strDa
ID: 3559136 • Letter: #
Question
(1) With an If:
...If intDay >= 0 AndAlso intDay <= 6 Then
......strName = strDayOfWeek(intDay)
...Else
......MessageBox.Show("Invalid day number.", "Error")
...End If
(2) With the length of the array (or upper bound):
...If intDay >= 0 AndAlso intDay <= strDayOfWeek.GetUpperBound(0) Then
......strName = strDayOfWeek(intDay)
...Else
......MessageBox.Show("Invalid day number.", "Error")
...End If
(3) With a Try..Catch statement:
...Try
......strName = strDayOfWeek(intDay)
...Catch ex As Exception
......MessageBox.Show("Invalid day number. Technical reason: " + ex.Message, "Error")
...End Try
Which of the three methods of detecting an invalid subscript would work best? Why?
Explanation / Answer
I think, Third method of detecting an invalid subscript is best. Because in this we try to get the value of day in "try" statement and if any error occurs in retrieving value then that will be handled in "catch" statement and appropriately giving the reason of error. while this is not acheived in none of the two abve cases. Though in rest of two aove cases the error will be displayed if we get the value ofintDay less than 0 or greater than 6 but if any other error occurs, that could not be handled in those cases,So I think,
THIRD METHOD IS BEST
because in catch block all errors will be captured and it would be more safe to use "try" and "catch"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.