Question: Do you use AndAlso and OrElse?

Published 23 March 04 02:24 PM | Scott Mitchell

By choice I program in C#, but this month I am working on a couple of consulting projects (hire me!) that use VB.NET. I find myself using the short-circuiting operators - AndAlso and OrElse - quite a bit. There are times when you'd need to use them if you wanted to do something like the following on one line of code:

If reader.Read() AndAlso reader(”FirstName”) = “Scott” then
End If

I don't find myself doing things like the above, but I still find myself using AndAlso and OrElse a lot. Simple things, like if I want to only proceed if two Boolean values are true, I'll do:

If bool1 AndAlso bool2 then ...

I'm curious if the everyday / regular VB.NET programmers find themselves using the short-circuiting operators or not. The only reason, I fathom, why you'd not want to use short-circuiting is if the evaluation you were performing had some side-effects, which is a bad programming practice. It's a real shame the VB.NET language designers buckled to the pressure of the VB community and didn't just made And and Or short-circuiting, as initially planned.

Filed under:


No Comments


My Books

  • Teach Yourself ASP.NET 4 in 24 Hours
  • Teach Yourself ASP.NET 3.5 in 24 Hours
  • Teach Yourself ASP.NET 2.0 in 24 Hours
  • ASP.NET Data Web Controls Kick Start
  • ASP.NET: Tips, Tutorials, and Code
  • Designing Active Server Pages
  • Teach Yourself Active Server Pages 3.0 in 21 Days

I am a Microsoft MVP for ASP.NET.

I am an ASPInsider.