locked
If Statement with NOT IN operator RRS feed

  • Question

  • Hi Guys, Good Day. Is it possible to use NOT IN operator in If Statement?

    i.e. If strPara NOT IN (strCompare1,strCompare2) then exit sub

     

    would that be possible?

     

    Thanks you in advanced.

     

    ________________________

    God Bless

    Charlie P.

    Thursday, March 31, 2011 6:25 AM

Answers

  • Hi Charlie,

    I think you have confused with list.contains and string.contains

    list.Contains doesn't perform like, It will search for exact string is exist in that list or not.

     

    Dim list As New List(Of String)
      list.Add("Stick Bread")
      list.Add("Coconut")
      If list.Contains("coconut") Then
       MessageBox.Show("yes")
      End If
    

     

    Look at the above code, the messagebox will not be displayed. The list is case sensitive also.

    I think this is what you are looking for. Try to execute the code provided by other members.

    Have a look by yourself, it will help in better understanding.


    Please mark the post as answer if it is helpfull to you - Hiran Repakula
    Friday, April 1, 2011 2:15 AM

All replies

  • Charlie,

    I don't know direct the expression which you use without the Not

    StrPara In (strCompare1,strCompare2)

    Do you have a link where you found that one.

     


    Success
    Cor
    Thursday, March 31, 2011 6:30 AM
  • Good Day Sir Cor.

     

    None Sir. Actually, I just thought about it. Could that be impossible? If so, It's OK Sir. Thank you by the way.

     

    _____________________

    God Bless

    Charlie P.

    Thursday, March 31, 2011 6:40 AM
  • you could add those strings in a list of strings

    dim list as new list(of string)

    list.add("param1")

    list.add("param2")

    ...

    if not list.Contains(StrPara ) then

    ...

    Thursday, March 31, 2011 7:31 AM
  • Hello Nico,

     

    Thank you so much. Nice idea. If you don't mind, I just wanna ask if what would be the value of strPara assuming param1 = "Redemption" and param2 = "Truth"?.

     

    Thank you.

     

    ____________________

    God Bless

    Charlie P.

    Thursday, March 31, 2011 9:08 AM
  • Please don't mix syntax with religion.

    Your question doesn't make sense to me.

    Thursday, March 31, 2011 9:36 AM
  • Charlie the general syntax for Not is in Visual Basic

    if Not (Charlie = "Pandacan") then 'be aware that for this mostly is used if Charlie <> "Pandacan"

      'do not this Charlie method
    end if

    or

    If Not Charlie is Human then

       'Charlie is not a Human

    end if

    or

    If Charlie IsNot Human then 'in these cases Human is an instanced 

        ' a little bit senseless but some found the first way ugly so they created the second one too as addition in framework 2

    End if

    As general rule, try to avoid Not statements they can become very confusing for instance this one

    If Not Charlie IsNot object then

    'It means that Charlie is something

    End if


    Success
    Cor
    Thursday, March 31, 2011 9:52 AM
  • There are so many ways to refuse. It is not hard to say words that are not offensive. A lot of questions here are much more non-sense than my question.

     

    I guess my question has a sense though. IN operation is possible in SQL Query then why not in If statement?. What the big difference? I am asking of what would be the strPara value look like because I am comparing it to SELECT * FROM Table WHERE Col NOT IN ('foo1', 'foo2').

     

    I was asking you in a polite way, If you don't like my question and samples then don't reply. I am here because I thought members are respectful, educated and insults are prohibited.

     

     

    Thursday, March 31, 2011 10:10 AM
  • Charly,

    I realy mean what I say: your question doen't make sense: you ask how to use 'NOT ' in an if-then-else statement,

    I provide a way to do that, and you ask me what would be the value of a variable? Huh?

    Thursday, March 31, 2011 10:17 AM
  • Hi,

    This forum is to help others, not to criticize someone. If you know something that dosent mean that every body has to know that.

    There may be people who will be beginners. Try to make the posts helpful.


    Please mark the post as answer if it is helpfull to you - Hiran Repakula
    Thursday, March 31, 2011 11:17 AM
  • Perhaps you are asking about this?
     
    dim list as new list(of string)
    list.add("Redemption")
    list.add("Truth")
     
    if (not list.Contains("Evil")) then
     ' do something...

     

    --
    Mike
    Thursday, March 31, 2011 12:13 PM
  • Charlie,

    Yes you can do the kind of statements you want, and that is with Linq, try to evaluate it 

    There are very fine samples for that.

    http://msdn.microsoft.com/en-us/vbasic/bb688088.aspx

    About your discussion with Nico, probably a joke he made which was not understood. 

    Happens often in the forums.

     


    Success
    Cor
    Thursday, March 31, 2011 12:24 PM
  • Hi Charlie Pandacan,

    Add one Button to a Form and try this code please.

    This idea can be extended for other collections but at the moment the Functions work for a LIST or an ARRAY.

     

    Public Class Form1
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
     Dim TheFlintstones As List(Of String) = (New String() {"Fred", "Barney", "Wilma"}).ToList
    
     If NotIn(TheFlintstones, "Bam Bam") = True Then
    
      MessageBox.Show("Bam Bam is NOT IN the list TheFlintstones!!")
    
     End If
    
     '-----------------------------------------------------------------
    
     Dim myNumbersArray() As Integer = New Integer() {1, 3, 5, 7, 9}
    
     If NotIn(myNumbersArray, 4) = True Then
    
      MessageBox.Show(" '4' is NOT IN myNumbersArray")
    
     End If
    
     End Sub
    
     Public Function NotIn(Of T)(ByVal aList As System.Collections.Generic.List(Of T), ByVal someObject As T) As Boolean
    
     If aList.Contains(someObject) = False Then
      Return True
     Else
      Return False
     End If
    
     End Function
    
     Public Function NotIn(Of T)(ByVal oneDimensionalArray() As T, ByVal someObject As T) As Boolean
    
     If oneDimensionalArray.Contains(someObject) = False Then
      Return True
     Else
      Return False
     End If
    
     End Function
    
    End Class
    

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7


    Thursday, March 31, 2011 10:19 PM
  • Hi again Charlie Pandacan,

    Forum user Family Tree Mike has mentioned the VB.Net alternative.

     

    If something is NOT IN something then the .Contains result = False

     

    .Contains is available for a String, a LIST and an ARRAY as well as other collection types.

     

    I hope this helps?    :-)    ;-)    :-D

     

    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim aString As String = "abcdefghij"
    
        If aString.Contains("z") = False Then
          MessageBox.Show(" 'z' is NOT IN " & aString)
        End If
    
      End Sub
    
    End Class
    



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Thursday, March 31, 2011 10:29 PM
  • Hello Hiran,

     

    Thank you Sir. That is what I am expecting here. I shifted from DELPHI to VB.Net  because there are a lot supports.

     

    ____________________

    God Bless

    Charlie P.

    Friday, April 1, 2011 1:18 AM
  • Hi Sir John Anthony,

     

    I got the point but I was just cautious of values that might have similar in substring. I am dealing with products that there are slight similar with their names.

     

    i.e.

    Dim list As New List(of String)

    list.add("Stick Bread")

    list.add("Coconut")

     

    If (list.Contains("Stick") then do something

     

    Or

     

    If (list.Contains("Coco") then do something

     
    I want to get the exact string not a similar string. My point is how .Contains Function will perform a comparison to a string, is it same as LIKE or EQUAL?.
    I think .Contains and IN are different in method of getting a value.
    Thank you Sir.
    ______________
    God Bless
    Charlie P.


    Friday, April 1, 2011 1:49 AM
  • Hi Charlie,

    I think you have confused with list.contains and string.contains

    list.Contains doesn't perform like, It will search for exact string is exist in that list or not.

     

    Dim list As New List(Of String)
      list.Add("Stick Bread")
      list.Add("Coconut")
      If list.Contains("coconut") Then
       MessageBox.Show("yes")
      End If
    

     

    Look at the above code, the messagebox will not be displayed. The list is case sensitive also.

    I think this is what you are looking for. Try to execute the code provided by other members.

    Have a look by yourself, it will help in better understanding.


    Please mark the post as answer if it is helpfull to you - Hiran Repakula
    Friday, April 1, 2011 2:15 AM
  • Thank you Sir Hiran. It is clear to me now.

     

    _________________________

    God Bless

    Charlie P.

    Friday, April 1, 2011 2:32 AM