Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi I have following values I want to add these in collection if the values already in collection message show this is already added in your collection..

`Dim OrdLines As New Collection`

`OrdLines.Add (111,this is first item)`

`OrdLines.Add (222,this is second item)`

`OrdLines.Add (333,this is third item)`

`OrdLines.Add (444,this is fourth item)`

please guide me how to avoid duplicate values in collection
thanks in advance
Posted

Create a loop and add data to your collection in the loop.
Then check if collection contains value show error,
Else add data to your collection..

VB
For Each input As var In inputList
    If OrdLines.Any(() => {  }, str.Item1.Contains(firstinputvalue)) Then
        ' show error
    Else
        OrdLines.Add(input)
    End If
Next
 
Share this answer
 
v4
Comments
Member 10258508 14-Sep-13 4:57am    
please give me example
You could check if the ordlines contains the values you want to add

VB
If OrdLines.Contains(555) Or OrdLines.Contains("This is the fifth item") Then
           MessageBox.Show("One of these items allready excists")
       Else
           OrdLines.Add(555, "This is the fifth item")
       End If


But when the value you want to add is 5555 then you would have a messagebox
showing that the value already excists.

To avoid that you could use an equals and check every value in your list

VB
For Each item In OrdLines
          If item.Equals(555) Then
              MessageBox.Show("555")
          ElseIf Not item.Equals(555) Then
              OrdLines.Add(555, "This is the fifth item")
          End If
      Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900