3

I have referred What is the difference between And and AndAlso in VB.net? to understand the difference between And and AndAlso.

I am from C# background - and C# has AndAlso only - no And.. And I don't see any practical use/advnatage of And.

Is there any practical use of And in VB.Net?

Reference

  1. Or versus OrElse
  2. What is the difference between And and AndAlso in VB.net?
10
  • 1
    AndAlso and OrElse are recent additions to VB.NET to cut down on the full evaluation of all of the clauses in a comparison. This addition brought VB.NET more in-line with C#'s comparison clauses. Jul 1, 2014 at 12:55
  • @AdamZuckerman What do you mean by "recent". In which version did get introduce?
    – LCJ
    Jul 1, 2014 at 12:59
  • 2
    AND was part of BASIC before VB or .NET wasn't it?
    – JeffO
    Jul 1, 2014 at 13:10
  • 1
    AndAlso was introduced in .NET 2003. Jul 1, 2014 at 19:30

2 Answers 2

5

C# doesn't have "And" and "Or". It has "&&" and "||".

"AndAlso" and "OrElse" were added to Visual Basic to provide a [new] syntax for "short-circuiting" the evaluation of conditions (which C# has always done, but Basic didn't). There was a lot of "discussion" early on about whether the existing operators should be reworked to support this but, for compatibility with existing code, new operators were added instead.

1
  • 5
    C# does have "And" and "Or", though, in the form of "&" and "|". Typically these are used for bit manipulation, but they perform exactly as expected on Boolean variables. They can be used for non-short-circuiting Boolean logic.
    – KChaloux
    Jul 1, 2014 at 13:25
1

There might be some reasons to evaluate both statements when dealing with sideeffects: if you want both to happen!

The best example I can come up right now is logging: you have two checks (both have to be true) and want to log both checks and their results (as a sideffect in the check) - in this case VB's AND is a good idea.

5
  • 2
    Only insofar as it's a good idea to save a couple of boolean variables by writing brittle code with non-obvious requirements which could easily be broken by a future developer making changes. Just call the two functions, store the results in variables, then do If Var1 And Var2 Jul 2, 2014 at 4:33
  • Why should this be more brittle than the way you descripe it? If the other developer does not understand the nuances of VB.net than maybe the problem lies there. The difference between And and AndAlso should be obvious to VB.net developers
    – Random Dev
    Jul 2, 2014 at 4:36
  • 1
    The school of thought that functions should avoid side-effects exists for a reason. If you think there's no risk of future programmer error in putting side effects into code that appears to purely be a boolean check, and relying on nobody accidentally replacing the intended-for-backward-compatibility-with-decade-old-code And with an AndAlso, then I guess we'll have to agree to disagree. Jul 2, 2014 at 4:49
  • LOL ... maybe you should look me up a bit - my main interests are FP and Haskell/Idris ;) ... I am the last to tell people to sprinkle sideeffects everywhere (indeed I would strongly advise to avoid them anywhere) - but that was not the question - the question was why And is left there at all (or at least that's my interpretation)
    – Random Dev
    Jul 2, 2014 at 5:00
  • BTW: where is your take on this?
    – Random Dev
    Jul 2, 2014 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.