Free Information Technology Magazines and eBooks

Tuesday, April 28, 2009

VB.NET: How to use AndAlso and OrElse

For those who don't know yet, there are new logical operators in VB.NET's town. They are AndAlso and OrElse. The new operators are basically shortcut versions of Multiple ANDs and ORs. On this post, I will try to explain the difference of the new operators from their traditional counterparts. To start lets have a look at the following VB.NET Code.



If lvInvoiceItems.SelectedItems.Count > 0 Then
If MessageBox.Show("This will delete " & lvInvoiceItems.SelectedItems(0).Text & "? Continue?","Confirm",MessageBoxButtons.YesNo ) = Windows.Forms.DialogResult.Yes Then
lvInvoiceItems.SelectedItems(0).Remove()
End If
End If


The code above is a typical IF condition that will check if there are selected items from lvInvoiceItems then ask for user confirmation before deleting the first selected item. Let's experiment and make the join the two IF condition. The following code can result to runtime error:


If lvInvoiceItems.SelectedItems.Count > 0 And MessageBox.Show("This will delete " & _ lvInvoiceItems.SelectedItems(0).Text & "?. Continue?","Confirm",MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
lvInvoiceItems.SelectedItems(0).Remove()
End If


When will it be runtime error? lvInvoiceItems.SelectedItems.Count = 0. Because both expression will be evaluated and since the selecteditem count is zero "lvInvoiceItems.SelectedItems(0).Text" will raise a runtime error. This is where AndAlso comes to the rescue. Take a look at the following revised code:


If lvInvoiceItems.SelectedItems.Count > 0 AndAlso MessageBox.Show("This will delete " & lvInvoiceItems.SelectedItems(0).Text & "?. Continue?","Confirm",MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
lvInvoiceItems.SelectedItems(0).Remove()
End If


If the first expression "lvInvoiceItems.SelectedItems.Count> 0" is false, the second IF expression will not be evaluated anymore (avoiding a runtime error).

The expression after AndAlso will only evaluated if the first expression is true.


Okay let's move to other new operator, OrElse is the shortcut of multiple OR condition. Lets enhance the code above and add some more validation:


If UserAccess = "ADMIN" OrElse UserAccess = "POWERUSER" Then
If lvInvoiceItems.SelectedItems.Count > 0 AndAlso MessageBox.Show("This will delete " & lvInvoiceItems.SelectedItems(0).Text & "?. Continue?","Confirm",MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
lvInvoiceItems.SelectedItems(0).Remove()
End If
End If


On traditional OR condition, both expressions/conditions will be evaluated. In OrElse, If the first expression is true, the second expression is not evaluated.

This kind of code technique is called short-circuiting. If the result of the first expression evaluated determines the final result of the operation, there is no need to evaluate the other expression, because it cannot change the final result. Short-circuiting can improve performance if the bypassed expression is complex, or if it involves procedure calls.



1 comments:

Dante MArk said...

I already know about this never understand its advantage till now. Cheer!