Solved

vb.net using and or andalso

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding bootstrap to only Content page and NOT master page 1 48
JSON  parse help 7 40
Iteration Help (Asp.net VB) 5 24
Upgrading my SSIS package in VS 2012 6 59
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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