When to use AndAlso vs And ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?UmljaA==?=

    #1

    When to use AndAlso vs And ?

    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
  • =?Utf-8?B?RGF2aWQgQW50b24=?=

    #2
    RE: When to use AndAlso vs And ?

    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
    Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

    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

    Comment

    • RobinS

      #3
      Re: When to use AndAlso vs And ?

      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).Ite ms(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" <Rich@discussio ns.microsoft.co mwrote in message
      news:82705A82-37C8-40E3-B613-64AAF8158802@mi crosoft.com...
      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

      Comment

      • aaron.kempf@gmail.com

        #4
        Re: When to use AndAlso vs And ?

        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).Ite ms(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" <Rich@discussio ns.microsoft.co mwrote in message
        news:82705A82-37C8-40E3-B613-64AAF8158802@mi crosoft.com...
        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

        Comment

        • Chris Dunaway

          #5
          Re: When to use AndAlso vs And ?

          aaron.kempf@gma il.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" <Rich@discussio ns.microsoft.co mwrote in message
          >
          When do I need to use AndAlso vs And?

          Comment

          • Chris Dunaway

            #6
            Re: When to use AndAlso vs And ?

            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

            Comment

            • aaron.kempf@gmail.com

              #7
              Re: When to use AndAlso vs And ?

              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:
              aaron.kempf@gma il.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" <Rich@discussio ns.microsoft.co mwrote in message

              When do I need to use AndAlso vs And?

              Comment

              • aaron.kempf@gmail.com

                #8
                Re: When to use AndAlso vs And ?

                you still don't understand the point.

                unnecessary change isn't good.

                it is unnecessary

                -Aaron


                Chris Dunaway wrote:
                aaron.kempf@gma il.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" <Rich@discussio ns.microsoft.co mwrote in message

                When do I need to use AndAlso vs And?

                Comment

                • Stephany Young

                  #9
                  Re: When to use AndAlso vs And ?

                  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" <dunawayc@gmail .comwrote in message
                  news:1167323721 .791014.145250@ n51g2000cwc.goo glegroups.com.. .
                  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
                  >

                  Comment

                  • aaron.kempf@gmail.com

                    #10
                    Re: When to use AndAlso vs And ?

                    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" <dunawayc@gmail .comwrote in message
                    news:1167323721 .791014.145250@ n51g2000cwc.goo glegroups.com.. .
                    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

                    Comment

                    • Chris Dunaway

                      #11
                      Re: When to use AndAlso vs And ?

                      aaron.kempf@gma il.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

                      Comment

                      • lord.zoltar@gmail.com

                        #12
                        Re: When to use AndAlso vs And ?


                        aaron.kempf@gma il.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".

                        Comment

                        • aaron.kempf@gmail.com

                          #13
                          Re: When to use AndAlso vs And ?

                          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.publi c.vb?

                          why did they bother changing the newsgroup name?

                          -Aaron


                          Chris Dunaway wrote:
                          aaron.kempf@gma il.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

                          Comment

                          • Bruce W. Darby

                            #14
                            Re: When to use AndAlso vs And ?

                            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" <dunawayc@gmail .comwrote in message
                            news:1167343324 .620302.74580@7 3g2000cwn.googl egroups.com...
                            aaron.kempf@gma il.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
                            >

                            Comment

                            • mg

                              #15
                              Re: When to use AndAlso vs And ?


                              aaron.kempf@gma il.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.

                              Comment

                              Working...