vendredi 25 octobre 2019

VB.Net - Can I check if a string IsNumeric, and if it is convert it to a number to be compared with, all within one IF statements condition?

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?

Aucun commentaire:

Enregistrer un commentaire