Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,222 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,233 people online right now. Registration is fast and FREE... Join Now!




Collection.contains not retruning what I expected

 
Reply to this topicStart new topic

Collection.contains not retruning what I expected

Mr Ed
8 Dec, 2007 - 02:51 PM
Post #1

New D.I.C Head
*

Joined: 9 Nov, 2007
Posts: 14


My Contributions
I expected to see just a blank screen, instead the loop does not find either value in col2.

What am I missing?

CODE

Module Module1
    Sub Main()
        Dim col1 As New Collection
        Dim col2 As New Collection
        Dim strValue As String = ""
        col1.Add("fred")
        col2.Add("fred")
        col1.Add("jane")
        col2.Add("jane")
        For Each strValue In col1
            If Not col2.Contains(strValue) Then
                Console.WriteLine(strValue & " not in col1")
            End If
        Next
        strValue = Console.ReadLine
    End Sub
End Module

User is offlineProfile CardPM
+Quote Post

f_soto
RE: Collection.contains Not Retruning What I Expected
8 Dec, 2007 - 05:50 PM
Post #2

New D.I.C Head
*

Joined: 20 Nov, 2007
Posts: 10


My Contributions
You were not missing much, just a change.. Try this...
wink2.gif
CODE

Module Module1
    Sub Main()
        Dim col1 As New Collection
        Dim col2 As New Collection
        Dim strValue As String '= ""
        col1.Add("fred")
        col2.Add("fred")
        col1.Add("jane")
        col2.Add("jane")
        For Each strValue In col1
            If Not col2.Contains(strValue) Then  'Delete Not and you will make it true causing it to display the else message
                'If this is not true then dont display anything
            Else
                Console.WriteLine(strValue & " Found in col1")
            End If
        Next
        strValue = Console.ReadLine
    End Sub
End Module

[/quote]

User is offlineProfile CardPM
+Quote Post

Mr Ed
RE: Collection.contains Not Retruning What I Expected
9 Dec, 2007 - 06:48 AM
Post #3

New D.I.C Head
*

Joined: 9 Nov, 2007
Posts: 14


My Contributions
Perhaps I am not seeing the forest for the trees. I changed the if..then to a if..then..else. Reading the code I expect to see 'fred is in col1 and jane is in col1' The opposite is what is happening. This is leading me to believe that the actual values are not being tested.

CODE

Module Module1
    Sub Main()
        Dim col1 As New Collection
        Dim col2 As New Collection
        Dim strValue As String = ""
        col1.Add("fred")
        col2.Add("fred")
        col1.Add("jane")
        col2.Add("jane")
        For Each strValue In col1
            If col2.Contains(strValue) Then
                Console.WriteLine(strValue & " is in col1")
            Else
                Console.WriteLine(strValue & " is not in col1")
            End If
        Next
        strValue = Console.ReadLine
    End Sub
End Module


User is offlineProfile CardPM
+Quote Post

mlahittette
RE: Collection.contains Not Retruning What I Expected
30 Nov, 2008 - 07:01 AM
Post #4

New D.I.C Head
*

Joined: 30 Nov, 2008
Posts: 1

Hi Ed,
the contains method works on the element key. Your code is adding elements with no keys. The key is always string and so are your elements, so you can use them as a key. This code should do what you want.

cheers,
Martin

CODE

Module Module1
    Sub Main()
        Dim col1 As New Collection
        Dim col2 As New Collection
        Dim strValue As String = ""
        col1.Add("fred", "fred")
        col2.Add("fred", "fred")
        col1.Add("jane", "jane")
        col2.Add("jane", "jane")
        For Each strValue In col1
            If col2.Contains(strValue) Then
                Console.WriteLine(strValue & " is in col1")
            Else
                Console.WriteLine(strValue & " is not in col1")
            End If
        Next
        strValue = Console.ReadLine
    End Sub
End Module


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:52AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month