The easiest way to learn the difference is by looking at a coding example in vb.net
dim a as object
case 1: using And
If (a isnot nothing) And (a.tostring = “”) Then
‘ do something here
End if
The only problem with the code above is that it will give an error when a.tostring=”” is evaluted since a is simply a declaration and doesn’t hold anything yet until it’s initialized, for example: a = new something
This could be eaily solved by using AndAlso which brings us to
case 2: Using AndAlso
If (a isnot nothing) AndAlso (a.tostring = “”) Then
‘ do something here
End if
In essence, what AndAlso does is that it ensures that the second condition (a.tostring = “”) is only evaluted if the first condition (a isnot nothing) is true.
The other way is to look at this as two seperate if else
if a isnot nothing then
if a.tostring = “” then
‘ do somrthing here
end if
end if
So that’s about it!
Leave a Reply