Reputation: 3
I have a database with a column labeled PrintOnPage. It can hold the values "ALL", "LAST", or a specific page number (1,2,3, etc), they are all stored as a string.
In my code I have the following IF statement:
If ((PrintOnPage.ToUpper = "ALL") OrElse
(PrintOnPage.ToUpper = "LAST" And iPageNo = (_indexDatabase.GetField("LastPage").Trim) OrElse
(IsNumeric(PrintOnPage) And CInt(PrintOnPage) = iPageNo)) Then
The last condition of the IF statement breaks if PrintOnPage is a string that cannot be converted to an INT. Because PrintOnPage can be LAST and iPageNo != to the last page specified in the database until the loop it is in iterated to the proper page, this is an issue.
Even if IsNumeric is False the second part of the condition after the AND is still checked, and thus breaks. I am trying to avoid nesting to many IF statements if I can.
Is there anyway to solve this issue within the same IF statement?
Upvotes: 0
Views: 206
Reputation: 6756
You can use AndAlso
in order to short-circuit your If
evaluation, just like you're using the OrElse
:
If ((PrintOnPage.ToUpper = "ALL") OrElse
(PrintOnPage.ToUpper = "LAST" AndAlso iPageNo = (_indexDatabase.GetField("LastPage").Trim) OrElse
(IsNumeric(PrintOnPage) AndAlso CInt(PrintOnPage) = iPageNo)) Then
This way, if the first part IsNumeric(PrintOnPage)
fails, it won't try to evaluate the following part(s).
Upvotes: 1