Solved

vb.net using and or andalso

Posted on 2013-10-29
4
219 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
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 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 69

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now