VB.net String Compare Not Equal

To compare to string in VB.net like many other language you can’t just use the equal sign = You have to use the .equal() function

Dim firstString As String = "test"
Dim secondString As String = "test"
If (firstString.Equals(secondString)) Then
   ' code
End If

Now to set this to not equal to you could simply use use Else. This aren’t beautiful and shouldn’t be used as this will be confusing when reading the code

If (firstString.Equals(secondString)) Then
Else
   ' code
End If

You should use the If Not instead like this:

Dim firstString As String = "test"
Dim secondString As String = "test"
If Not (firstString.Equals(secondString)) Then
   ' code
End If

Note the different from the first. As this will be true if the strings are not equal to each other.

Posted in Tips and Tricks, Windows Tagged with: , , ,