Solved

vb.net using and or andalso

Posted on 2013-10-29
4
220 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 75

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 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

13 Experts available now in Live!

Get 1:1 Help Now