435,572 Members | 2,949 Online
Need help? Post your question and get tips & solutions from a community of 435,572 IT Pros & Developers. It's quick & easy.

# When to use AndAlso vs And ?

 P: n/a Greetings, If x = y And m = n The .... End If If x = y AndAlso m = n Then .... End If When do I need to use AndAlso vs And? Thanks, Rich Dec 28 '06 #1
30 Replies

 P: n/a For the sample you provided, you are doing a strictly logical operation. Use the "AndAlso" since it's more efficient (it doesn't evaluate the second operand if the first is false). The "And" operator is usually just used for bitwise operations. You can also use "And" for logical operations, but it evaluates both operands always. -- David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C#/VB to C++ converter Instant Python: VB to Python converter "Rich" wrote: Greetings, If x = y And m = n The ... End If If x = y AndAlso m = n Then ... End If When do I need to use AndAlso vs And? Thanks, Rich Dec 28 '06 #2

 P: n/a If you use "And", then it processes all of the clauses and then decides if it's true or not. So it checks both (x=y) and (m=n). If you use "AndAlso", it stops processing when it hits a condition that is false. So if (x=y) is false, it never checks (m=n). This is more efficient because it doesn't do unnecessary processing, and also can be handy, if you want to do something like this: If (rdr IsNot Nothing) AndAlso rdr.Rows(0).Items(0).ToString = "abc" ... Unlike VB6, it won't crap out if rdr is nothing, because it won't process the second clause. For the "or" operator, they added "OrElse" which does the same thing. It's called short-circuiting. I believe this is the way "and" and "or" work in C/C++/C#. I think they would have just changed "and" and "or" in VB, but it would have freaked out too many people and required more work when converting from VB6, so they just added new constructs. Robin S. (more info than you wanted, wasn't it?) ------------------------------ "Rich"

 P: n/a what a load of crap it worked well enough in VB6 unnecessary change isn't good. it's unnecessary -Aaron RobinS wrote: If you use "And", then it processes all of the clauses and then decides if it's true or not. So it checks both (x=y) and (m=n). If you use "AndAlso", it stops processing when it hits a condition that is false. So if (x=y) is false, it never checks (m=n). This is more efficient because it doesn't do unnecessary processing, and also can be handy, if you want to do something like this: If (rdr IsNot Nothing) AndAlso rdr.Rows(0).Items(0).ToString = "abc" ... Unlike VB6, it won't crap out if rdr is nothing, because it won't process the second clause. For the "or" operator, they added "OrElse" which does the same thing. It's called short-circuiting. I believe this is the way "and" and "or" work in C/C++/C#. I think they would have just changed "and" and "or" in VB, but it would have freaked out too many people and required more work when converting from VB6, so they just added new constructs. Robin S. (more info than you wanted, wasn't it?) ------------------------------ "Rich"

 P: n/a aa*********@gmail.com wrote: what a load of crap it worked well enough in VB6 Really? How would VB6 handle this code if the instance were null? If (Not obj Is Nothing) And (obj.Property = "something") Then 'Do something here End If It would have crashed because IIRC, And was not short circuited in VB6 .. Using AndAlso avoids this in VB.Net. > unnecessary change isn't good. it's unnecessary The original VB.Net beta proposed adding two new operators called BitAnd and BitOr which would be used for bitwise operations and changing And and Or to only be logical operations. That would have been an unnecessary change and, thankfully, they changed it back. > "Rich" When do I need to use AndAlso vs And? Dec 28 '06 #5

 P: n/a Rich wrote: > When do I need to use AndAlso vs And? RobinS explained how those keywords work so I won't repeat that information. My recommendation is to ALWAYS use AndAlso and OrElse when doing logical comparisons. It costs you nothing to use them and you get the benefits of short circuiting. Use And and Or for bitwise operations. The exception being if you are reusing code from VB6 and it might not be worth the time to change the And's and Or's to AndAlso and OrElse. Chris Dec 28 '06 #6

 P: n/a and you seriously can't find a workaround for that? I'd wrap it in a function and be done with it. lol shit can you seriously not figure out the logic here? Public Function DoSomething(obj as object) as boolean on error resume next If obj.property <"something" DoSomething = False Else DoSomething = True end select End function Chris Dunaway wrote: aa*********@gmail.com wrote: what a load of crap it worked well enough in VB6 Really? How would VB6 handle this code if the instance were null? If (Not obj Is Nothing) And (obj.Property = "something") Then 'Do something here End If It would have crashed because IIRC, And was not short circuited in VB6 . Using AndAlso avoids this in VB.Net. unnecessary change isn't good. it's unnecessary The original VB.Net beta proposed adding two new operators called BitAnd and BitOr which would be used for bitwise operations and changing And and Or to only be logical operations. That would have been an unnecessary change and, thankfully, they changed it back. > "Rich"

 P: n/a you still don't understand the point. unnecessary change isn't good. it is unnecessary -Aaron Chris Dunaway wrote: aa*********@gmail.com wrote: what a load of crap it worked well enough in VB6 Really? How would VB6 handle this code if the instance were null? If (Not obj Is Nothing) And (obj.Property = "something") Then 'Do something here End If It would have crashed because IIRC, And was not short circuited in VB6 . Using AndAlso avoids this in VB.Net. unnecessary change isn't good. it's unnecessary The original VB.Net beta proposed adding two new operators called BitAnd and BitOr which would be used for bitwise operations and changing And and Or to only be logical operations. That would have been an unnecessary change and, thankfully, they changed it back. > "Rich"

 P: n/a While I concur with the 'use AndAlso and OrElse when doing logical comparisons' rule of thumb, as usual there are always exceptions to the rule. One such exception is the situation where one has 2 functions where both return a boolean, both execute some code that is critical to the application and the result of both must be True for some other action to be taken, e.g.: Private Sub Form_Load(...) Handles MyBase.Load If FunctionA() And FunctionB() Then ' Take some critical action before continuing End If End Sub Private Function FunctionA() As Boolean Try ' Some critical code here Return True Catch Return False End Try End Function Private Function FunctionB() As Boolean Try ' Some other critical code here Return True Catch Return False End Try End Function If AndAlso was used in this case, and FunctionA returned False then FunctionB would not be called and a critical step would be 'missed'. I am not advocation that this is a good way to go about things, merely that it demonstrates a situation where 'short-circuiting' is not desirable. "Chris Dunaway" >When do I need to use AndAlso vs And? RobinS explained how those keywords work so I won't repeat that information. My recommendation is to ALWAYS use AndAlso and OrElse when doing logical comparisons. It costs you nothing to use them and you get the benefits of short circuiting. Use And and Or for bitwise operations. The exception being if you are reusing code from VB6 and it might not be worth the time to change the And's and Or's to AndAlso and OrElse. Chris Dec 28 '06 #9

 P: n/a what a load of crap it worked well enough in VB6 unnecessary change isn't good. it's unnecessary Stephany Young wrote: While I concur with the 'use AndAlso and OrElse when doing logical comparisons' rule of thumb, as usual there are always exceptions to the rule. One such exception is the situation where one has 2 functions where both return a boolean, both execute some code that is critical to the application and the result of both must be True for some other action to be taken, e.g.: Private Sub Form_Load(...) Handles MyBase.Load If FunctionA() And FunctionB() Then ' Take some critical action before continuing End If End Sub Private Function FunctionA() As Boolean Try ' Some critical code here Return True Catch Return False End Try End Function Private Function FunctionB() As Boolean Try ' Some other critical code here Return True Catch Return False End Try End Function If AndAlso was used in this case, and FunctionA returned False then FunctionB would not be called and a critical step would be 'missed'. I am not advocation that this is a good way to go about things, merely that it demonstrates a situation where 'short-circuiting' is not desirable. "Chris Dunaway" When do I need to use AndAlso vs And? RobinS explained how those keywords work so I won't repeat that information. My recommendation is to ALWAYS use AndAlso and OrElse when doing logical comparisons. It costs you nothing to use them and you get the benefits of short circuiting. Use And and Or for bitwise operations. The exception being if you are reusing code from VB6 and it might not be worth the time to change the And's and Or's to AndAlso and OrElse. Chris Dec 28 '06 #10

 P: n/a aa*********@gmail.com wrote: you still don't understand the point. unnecessary change isn't good. it is unnecessary I do understand the point, and I agree that unnecessary change isn't good. And and Or STILL function like they did before!! Nothing has changed, a feature has been ADDED. Chris Dec 28 '06 #11

 P: n/a aa*********@gmail.com wrote: what a load of crap it worked well enough in VB6 unnecessary change isn't good. it's unnecessary Ok so maybe change just for the sake of change is sometimes a Bad Thing, but I don't see a problem with having short-ciruit logic operators. It's not as if they are the ONLY logic operator, you can still use "And" and "Or". Dec 28 '06 #12

 P: n/a it's not a feature.. it's a point of confusion VB.net has NO TANGIBLE BENEFITS over VB6. the IDE sucks; it is slower it is more difficult; it doesn't produce portable code. I used to be able to write VB6 code, cut and paste it into Notepad and run it as a VBS file. When VB.net comes 90% close to being able to have this sort of simplicity- is when I lower myself to using VB 2005. and the point of the matter is that it's not called VB.net 2005; it is merely called VB 2005. Why are we posting in a newsgroup named DOTNET when we're not talking about a product named DOTNET? why are there 40 dead newsgroups at microsoft.public.vb? why did they bother changing the newsgroup name? -Aaron Chris Dunaway wrote: aa*********@gmail.com wrote: you still don't understand the point. unnecessary change isn't good. it is unnecessary I do understand the point, and I agree that unnecessary change isn't good. And and Or STILL function like they did before!! Nothing has changed, a feature has been ADDED. Chris Dec 28 '06 #13

 P: n/a But Chris, If you use logic in this way, he can't continue to rail against the machine, tossing his little tantrums because he needs to change his way of thinking and coding Bruce "Chris Dunaway" you still don't understand the point.unnecessary change isn't good.it is unnecessary I do understand the point, and I agree that unnecessary change isn't good. And and Or STILL function like they did before!! Nothing has changed, a feature has been ADDED. Chris Dec 29 '06 #14

 P: n/a aa*********@gmail.com wrote: what a load of crap it worked well enough in VB6 unnecessary change isn't good. it's unnecessary yawn. If this is the limits of your contribution, don't bother MB. Dec 29 '06 #15

 P: n/a I don't need to change my way of coding. I'll stay happy, wellpaid in the database world and say screw programming -Aaron Bruce W. Darby wrote: But Chris, If you use logic in this way, he can't continue to rail against the machine, tossing his little tantrums because he needs to change his way of thinking and coding Bruce "Chris Dunaway"

 P: n/a Take a look at : http://msdn2.microsoft.com/en-us/lib...fz(VS.80).aspx http://msdn2.microsoft.com/en-us/lib...8a(VS.80).aspx http://blogs.msdn.com/vbfaq/archive/...20/116591.aspx -- Moayad Mardini, MSDN Forums Moderator "unknown" wrote: > Dec 30 '06 #17

 P: n/a aa*********@gmail.com wrote: it's not a feature.. it's a point of confusion VB.net has NO TANGIBLE BENEFITS over VB6. the IDE sucks; it is slower it is more difficult; it doesn't produce portable code. I used to be able to write VB6 code, cut and paste it into Notepad and run it as a VBS file. When VB.net comes 90% close to being able to have this sort of simplicity- is when I lower myself to using VB 2005. and the point of the matter is that it's not called VB.net 2005; it is merely called VB 2005. Why are we posting in a newsgroup named DOTNET when we're not talking about a product named DOTNET? why are there 40 dead newsgroups at microsoft.public.vb? why did they bother changing the newsgroup name? -Aaron Chris Dunaway wrote: aa*********@gmail.com wrote: you still don't understand the point. > unnecessary change isn't good. > it is unnecessary I do understand the point, and I agree that unnecessary change isn't good. And and Or STILL function like they did before!! Nothing has changed, a feature has been ADDED. Chris VB.NET allows more use of operating system functions than VB6. A pitfall in VBS is that malicious people use it to wreak havoc on your system. Because of this, a lot of OS's prompt you before they run the script. This gets pretty annoying sometimes. Also, there are more IDE's out there than just the one Microsoft has created. there is a good one at icsharpcode.net It is called SharpDevelop. It was written entirely of C#.NET code. It can be used to write VBNET C++NET & C#NET. mediocrevbprogrammer.freed Dec 30 '06 #18

 P: n/a What about AndNotAlsoProvided? George Chris Dunaway wrote: Rich wrote: When do I need to use AndAlso vs And? RobinS explained how those keywords work so I won't repeat that information. My recommendation is to ALWAYS use AndAlso and OrElse when doing logical comparisons. It costs you nothing to use them and you get the benefits of short circuiting. Use And and Or for bitwise operations. The exception being if you are reusing code from VB6 and it might not be worth the time to change the And's and Or's to AndAlso and OrElse. Chris Dec 31 '06 #20

 P: n/a aa*********@gmail.com wrote: real quick-- my 7 machines; how do i determine which versions of the framework I _PROBABLY_ have installed on these 7 machines? keep in mind that it is 3 different operating systems :) I'm not fortunate enough to have even 3 machines, let alone 7, but I can just navigate to C:\WINDOWS\Microsoft.NET\Framework and sure there it is: 09/11/2006 00:20

 P: n/a wrong answer buck-o I've got several dual-boot machines -Aaron Branco Medeiros wrote: aa*********@gmail.com wrote: real quick-- my 7 machines; how do i determine which versions of the framework I _PROBABLY_ have installed on these 7 machines? keep in mind that it is 3 different operating systems :) I'm not fortunate enough to have even 3 machines, let alone 7, but I can just navigate to C:\WINDOWS\Microsoft.NET\Framework and sure there it is: 09/11/2006 00:20

 P: n/a in the good old days.. did it even fucking matter what version of ADO you were using? Seriously 2000 shipped with 2.5 and XP shipped with 2.6 right? other machines all had 2.1 with the release of IE4.. right? you are missing something; you got your head on backwards -Aaron Branco Medeiros wrote: aa*********@gmail.com wrote: real quick-- my 7 machines; how do i determine which versions of the framework I _PROBABLY_ have installed on these 7 machines? keep in mind that it is 3 different operating systems :) I'm not fortunate enough to have even 3 machines, let alone 7, but I can just navigate to C:\WINDOWS\Microsoft.NET\Framework and sure there it is: 09/11/2006 00:20

 P: n/a aa*********@gmail.com wrote: I've got several dual-boot machines Forgive my ignorance, but how does this inffluence anything in finding the installed version(s) of the .Net framework? Regards, Branco. Jan 1 '07 #24

 P: n/a aa*********@gmail.com wrote: in the good old days.. did it even fucking matter what version of ADO you were using? That's exactly my point... Regards, Branco. Jan 1 '07 #25

 P: n/a Unless you have a desired side effect of all parts of the expression getting performed reguardless so that a function gets called. However, This sort of "side effect" programming, is questionable at best and can be harder to debug for others. Usually it's best to short circuit. This is a great addition to VB and I use it alot. I used to have to jump some whoops to use AND because each item would get evaluated reguardless. For example: If not objectX is nothing AND objectX.method=something then This would blow up if objectX was nothing, because it would always try to hit part two of the condition. The same thing written like: If not objectX is nothing ANDALSO objectX.method=something then would have no problem because if objectX is nothing then it would stop before trying to access a method/property on it. -Shane Rich wrote: > When do I need to use AndAlso vs And? RobinS explained how those keywords work so I won't repeat thatinformation. My recommendation is to ALWAYS use AndAlso and OrElsewhen doing logical comparisons. It costs you nothing to use them andyou get the benefits of short circuiting.Use And and Or for bitwise operations.The exception being if you are reusing code from VB6 and it might notbe worth the time to change the And's and Or's to AndAlso and OrElse.Chris Jan 2 '07 #26

 P: n/a because C:\Program Files or C:\Windows doesn't point to the correct instance the point of the matter is that there is not a simple way to determine the current version of .NET framework on any of these 8 machines. THUS, .NET deployment is absolutely, prohibitively-- unreasonable. If every single machine in the world had .NET 3.0 then everything might peachy-- if it didn't run soooooo goddamn slow. but as it is; they're never going to make 3.0 run on Windows 2000 so they can eat a dick sandwich there is a better place; it is called PHP, MS killed VB, sorry guys -Aaron Branco Medeiros wrote: aa*********@gmail.com wrote: I've got several dual-boot machines Forgive my ignorance, but how does this inffluence anything in finding the installed version(s) of the .Net framework? Regards, Branco. Jan 2 '07 #27

 P: n/a I don't have a desired side effect. shit worked fine in the past. unnecessary change is not GOOD. it is not WANTED. it is not ACCEPTABLE. -Aaron sstory wrote: Unless you have a desired side effect of all parts of the expression getting performed reguardless so that a function gets called. However, This sort of "side effect" programming, is questionable at best and can be harder to debug for others. Usually it's best to short circuit. This is a great addition to VB and I use it alot. I used to have to jump some whoops to use AND because each item would get evaluated reguardless. For example: If not objectX is nothing AND objectX.method=something then This would blow up if objectX was nothing, because it would always try to hit part two of the condition. The same thing written like: If not objectX is nothing ANDALSO objectX.method=something then would have no problem because if objectX is nothing then it would stop before trying to access a method/property on it. -Shane

 P: n/a aa*********@gmail.com wrote: because C:\Program Files or C:\Windows doesn't point to the correct instance I stand corrected: the simplest way to check the installed .Net frameworks is to navigate to %windir%\microsoft.net\framework. Regards, Branco. Jan 3 '07 #29

 P: n/a does the windir variable work on Windows 98? -Aaron Branco Medeiros wrote: aa*********@gmail.com wrote: because C:\Program Files or C:\Windows doesn't point to the correct instance I stand corrected: the simplest way to check the installed .Net frameworks is to navigate to %windir%\microsoft.net\framework. Regards, Branco. Jan 3 '07 #30

 P: n/a aa*********@gmail.com wrote: does the windir variable work on Windows 98? Yes. B. Jan 4 '07 #31

### This discussion thread is closed

Replies have been disabled for this discussion.