• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

Has AndAlso made And obsolete

In recent questions AndAlso was brought up.   The question here is: Is there any reason to ever use the AND operator anymore. 500 points for the first person to come up with a situation where AND can be used but AndAlso couldn't.

Corey2
0
Corey Scheich
Asked:
Corey Scheich
1 Solution
 
Erick37Commented:
It matters if you call functions in your evaluation and you need BOTH functions to be called.

e,g,

If DoImportantStuff() = True And DoMoreImportantStuff() = True Then ...

Here BOTH functions are called, even if the first one evaluates false
0
 
b1xml2Commented:
I consider the And as obsolete and would urge the use of AndAlso as well as OrElse.

Of particular significance where you absolutely need to ensure the conditionality is evaluated from left to right.

Consider this:

Dim dt As DataTable
dt = Nothing
If Not dt Is Nothing And dt.Rows.Count > 0 Then

End If

An error will ensue as dt is nothing (null) and therefore dt.Rows is not valid.



in classic VB, you'd have to nest the if statements to
If Not dt Is Nothing
  If dt.Rows.Count > 0 Then
  ' code
  End If
End If

in VB.NET
If Not dt Is Nothing AndAlso dt.Rows.Count > 0 Then
'code
End If

allows for a graceful execution of conditionality and aligns it to C#,C++,Java which always evaluates conditionality from left to right. In short, VB.NET grew, amd improved.
e.g.
C#
If (dt != null && dt.Rows.Count > 0)
{

}

would be akin to the AndAlso example
0
 
S-TwilleyCommented:
I agree with Erick37 on this one though...


Function Task1(Byval SomeString() As String) As Boolean
     ' goes thru the array SomeString
     ' compares it to contents of listbox1... adds unique entries to listbox
     ' if values were added, return True, else Return False
End Function

Function Task2(Byval SomeString() As String) As Boolean
     ' goes thru the array SomeString
     ' compares it to contents of listbox2... adds unique entries to listbox
     ' if values were added, return True, else Return False
End Function


Sub SomeSub()
      If Task1(myArray1) And Task2(myArray2) Then
           Msgbox("New entries were added to both listboxes")
      Else
           Msgbox("Neither listboxes were updated, or only one was")
      End If
End Sub

==================

you still want BOTH methods to be called to update the listbox... if you used AndAlso, you might risk not calling the 2nd method
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
b1xml2Commented:
I'd prefer something like this:

Private Function Task1(ByVal values As String()) As Boolean
       ' goes thru the array SomeString
     ' compares it to contents of listbox1... adds unique entries to listbox
     ' if values were added, return True, else Return False
    End Function

    Private Function Task2(ByVal values As String()) As Boolean
        ' goes thru the array SomeString
     ' compares it to contents of listbox2... adds unique entries to listbox
     ' if values were added, return True, else Return False
    End Function

    Private Function Task3(ByVal values As String()) As Boolean
 ' goes thru the array SomeString
     ' compares it to contents of listbox3... adds unique entries to listbox
     ' if values were added, return True, else Return False
    End Function

    Private Function ProcessTasks() As Boolean
        Dim value As Boolean = True
        Dim tasks As TaskHandler() = New TaskHandler() {AddressOf Task1, AddressOf Task2, AddressOf Task3}
        For Each task As TaskHandler In tasks
            If Not task(Nothing) AndAlso Not value Then value = False
        Next
        Return value
    End Function

Sub SomeSub()
  If ProcessTasks() Then
    Msgbox("New entries were added to both listboxes")
  Else
   Msgbox("Neither listboxes were updated, or only one was")
  End If

End Sub

This way, you can extend your tasks be they 2 or 200 through an ArrayList or Array... your example is not robust enough


0
 
b1xml2Commented:
amendment
========
For Each task As TaskHandler In tasks
  If Not task(Nothing) AndAlso value Then value = False
Next
0
 
b1xml2Commented:
forgot to post the delegate signature:

Public Delegate Function TaskHandler(ByVal values As String()) As Boolean
0
 
S-TwilleyCommented:
im not denying that andalso could do it, but And is a much simpler solution for THAT case i specified.. just as AndAlso is a simpler solution for where Y must be calculated only if X is true

If X Then
      If y Then

      End If
End If
0
 
S-TwilleyCommented:
AND can be used in a simple statement in some cases... where ANDALSO needs to have extra lines added to give the same output

but ANDALSO for other uses can be expressed in a single statement which would take AND more lines.
0
 
Corey ScheichDeveloperAuthor Commented:
True to the origional post I handed Erick37 the points, though I don't think I would put 2 necessary calculations in an if statement I would prefer to evaluate them beforehand and then compare.  Thank you all for your opinions on the matter great discussion.

Corey2
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now