user1342164 user1342164 - 6 months ago 10
Vb.net Question

Input string was not in a correct format when doing string comparison

I am trying to evaluate if a user account equals a certain string that it does not continue. For example, if the user account is AMTEST or PMTEST that it does not execute the code in the if statement. Using the code below I am getting the error that "Input string was not in a correct format."

Something wrong with my syntax?

If UserAccount <> "AMTEST" Or "PMTEST" Or "TESTUSER" Then
' do something
End If

Answer

You have to use this syntax:

If UserAccount <> "AMTEST" AndAlso UserAccount <> "PMTEST" AndAlso UserAccount <> "TESTUSER" Then

End If

easier to read and to maintain would be this approach:

Dim testAccs = { "AMTEST", "PMTEST", "TESTUSER" }
If Not testAccs.Contains( UserAccount ) Then

End If