?
Solved

vb.net using and or andalso

Posted on 2013-10-29
4
Medium Priority
?
235 Views
Last Modified: 2013-10-29
Hello,
I would like to know what are the options of using And or AndAlso to get the same results that I have here..

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        If MyIPAddress = "0" Then
            If MyIPAddress = OldIPAddress Then
                If MyIPAddress.StartsWith("168.") Then
                    If MyIPAddress.StartsWith("169.") Then
                        If MyIPAddress.StartsWith("192.") Then

                                Debug.Print("all good")

                        End If
                    End If
                End If
            End If
        End If
    End Sub

Open in new window

0
Comment
Question by:XK8ER
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39608513
here:
    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        If MyIPAddress = "0" AndAlso 
MyIPAddress = OldIPAddress AndAlso  
MyIPAddress.StartsWith("168.") AndAlso 
MyIPAddress.StartsWith("169.") AndAlso 
MyIPAddress.StartsWith("192.") Then
                                Debug.Print("all good")

                        End If
                    End If
                End If
            End If
        End If
    End Sub

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39608521
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    If MyIPAddress = "0" AndAlso _
       MyIPAddress = OldIPAddress AndAlso _
       MyIPAddress.StartsWith("168.") AndAlso _
       MyIPAddress.StartsWith("169.") AndAlso _
       MyIPAddress.StartsWith("192.") Then
        Debug.Print("all good")
    End If
End Sub

Open in new window


And would do the same thing, except that And would evaluate *every* condition, whereas AndAlso will "short-circuit" on the first non-true condition. If any condition is False, then there is no reason to check the remaining conditions. In boolean algebra, in order for a boolean and to result in true, every condition must be true. If any single condition is false, then the overall result will be false. This is what "short-circuiting" accomplishes.
0
 
LVL 8

Expert Comment

by:5teveo
ID: 39608538
Not sure logic above makes sense...
If MyIPAddress = "0" then address can not be another value to test inside If block...

Maybe...
     
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        If MyIPAddress <> "0" Then
            If MyIPAddress <> OldIPAddress Then
                If MyIPAddress.StartsWith("168.") OR _
                   MyIPAddress.StartsWith("169.") OR _
                   MyIPAddress.StartsWith("192.") Then
                                Debug.Print("all good")
                End If
            End If
       End If
    End Sub
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39609358
you should not use or/and (but prefer orelse/andalso).

the explanation is at http://emoreau.com/Entries/Articles/2008/04/Short-circuiting-mainly-in-VBNet-and-SQL-Server.aspx
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question