David
David

Reputation: 15

vb.net - use of OR along with an equals to compare one variable's value with 3 others

It seems like this should be quick and easy, but I can't find the syntax to make it happen and haven't had any succesful searches.

I've written a program that grabs 3 UserIDs from one account, and 3 userIDs from another account to compare ownership of accounts and allow certain rights, depending on the ownership overlap. If a user ID is null, then a "0" is placed as the value of the UserID to avoid string to long conversion issues.

As it is currently written, I've tested when UserID1 matches 2UserID1, and there are no other users, and the program works correctly. I've also tested when userID1 and userID2 match 2userID1 and 2userID2, with no third user, but it works incorrectly. I have a feeling that the xxx = (yyy or zzz or uuu) is only looking at yyy and not bumping to the other values. Is there a way to make user1 compare it's value to the 3 different 2userIDx values without writing a series of or statements? I was hoping to streamline the code with what I have below.

       Dim userID1 As String = dr.GetString(1).Replace("-", "")
       Dim userID2 As String = dr.GetString(2).Replace("-", "")
       Dim userID3 As String = dr.GetString(3).Replace("-", "")
       Dim Account2 As String = dr.GetString(4)
       Dim 2userID1 As String = dr.GetString(5).Replace("-", "")
       Dim 2userID2 As String = dr.GetString(6).Replace("-", "")
       Dim 2userID3 As String = dr.GetString(7).Replace("-", "")

                  If userID1 = (2userID1 Or 2userID2 Or 2userID3) And userID2 = (2userID1 Or 2userID2 Or 2userID3) And userID3 = (2userID1 Or 2userID2 Or 2userID3)) Then
                    MessageBox.Show(fullACN.ToString + " and " + crACN.ToString + " can be crossed.")
                ElseIf (userID1 = (2userID1 Or 2userID2 Or 2userID3) And userID2 = (2userID1 Or 2userID2 Or 2userID3) And userID3 = "0") Then
                    MessageBox.Show(fullACN.ToString + " and " + crACN.ToString + " can be crossed.")
                ElseIf (userID1 = (2userID1 Or 2userID2 Or 2userID3) And userID2 = "0" And userID3 = "0") Then
                    MessageBox.Show(fullACN.ToString + " and " + crACN.ToString + " can be crossed.")
                Else
                    MessageBox.Show(fullACN.ToString + " and " + crACN.ToString + " cannot be crossed.")
                End If

            End If

Upvotes: 0

Views: 1917

Answers (4)

David
David

Reputation: 15

I modified and searched after Vishal's post and ended up using a mix of OrElse and AndAlso statements. Here is what I ended up with, and has returned correct results on the first 20 tests. Thanks to Andrew Mortan and the_Lotus for their input. I didn't want to use arrays so that I can grab variables easily if needed. I need to work on my use of arrays for the future.

If ((userid1 = user2id1 OrElse userid1= user2id2 OrElse userid1= user2id3) AndAlso (userid2= user2id2 OrElse userid2= user2id1 OrElse userid2= user2id3) AndAlso (userid3= user2id3OrElse userid3= user2id1 OrElse userid3= user2id2)) Then
                    MessageBox.Show(fullACN.ToString + " and " + crACN.ToString + " can be crossed.")
                ElseIf ((userid1= user2id1 OrElse userid1= user2id2 OrElse mbrssn = user2id3) AndAlso (userid2= user2id2 OrElse userid2= user2id1 OrElse userid2= user2id3) AndAlso (userid3= "0")) Then
                    MessageBox.Show(fullACN.ToString + " and " + crACN.ToString + " can be crossed.")
                ElseIf ((userid1= user2id1 OrElse userid1= user2id2 OrElse userid1= user2id3) AndAlso (userid2= "0") AndAlso (userid3= "0")) Then
                    MessageBox.Show(fullACN.ToString + " and " + crACN.ToString + " can be crossed.")
                Else
                    MessageBox.Show(fullACN.ToString + " and " + crACN.ToString + " cannot be crossed.")
                End If

Upvotes: 0

Bill
Bill

Reputation: 4585

the_lotus is right about syntax. You need to break the comparisons up into individual OR statements. If you really want to eliminate the duplicate ORs from the code, you can use a Select Case like this. This code will return "Case 2"

Dim str As String = "abc"
Select Case str
    Case "qwer", "wert", "erty"
        System.Console.WriteLine("Case 1")
    Case "xcvb", "abc"
        System.Console.WriteLine("Case 2")
    Case Else
        System.Console.WriteLine("Case 3")
End Select

Upvotes: 0

the_lotus
the_lotus

Reputation: 12748

You can't have numbers at the beginning of variable

You aren't doing the If correctly, insted of

userID1 = (2userID1 Or 2userID2 Or 2userID3)

It should be

(userID1 = 2userID1 Or userID1 = 2userID2 Or userID1 = 2userID3)

If you don't want to do a lot of Or, you'll have to put the values in an array

Dim dbValue As New List(Of String)

dbValue.Add(dr.GetString(5).Replace("-", ""))
dbValue.Add(dr.GetString(6).Replace("-", ""))
dbValue.Add(dr.GetString(7).Replace("-", ""))

If dbValues.Contains(userID1) Then

Upvotes: 1

Andrew Morton
Andrew Morton

Reputation: 25023

I think this is what you want:

Dim user1 As New List(Of String)
Dim user2 As New List(Of String)

For i = 1 To 3
    user1.Add(dr.GetString(i).Replace("-", ""))
Next

Dim account2 As String = dr.GetString(4)

For i = 5 To 7
    user2.Add(dr.GetString(i).Replace("-", ""))
Next

If user1.Intersect(user2).Count > 0 Then
    ' they have a common userID
End If

Upvotes: 0

Related Questions