Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dim numbers As New List(Of String)()
       numbers.Add("One")
       numbers.Add("Two")
       numbers.Add("Three")

       If Not numbers.Contains("on5e") = False Then
           MsgBox("not exist")
       Else
           MsgBox("exist")
       End If


What I have tried:

i need to get true or false
i getting error on both
Posted
Updated 3-Jan-21 20:23pm

Try like this:
Imports System
Imports System.Collections.Generic
				
Public Module Module1
	Public Sub Main()
		
		Dim numbers As New List(Of String)
       numbers.Add("One")
       numbers.Add("Two")
       numbers.Add("Three")

       If Not numbers.Contains("One") Then
			Console.WriteLine("not exist")
       Else
			Console.WriteLine("exist")
       End If
	End Sub
End Module
 
Share this answer
 
Quote:
i need to get true or false
i getting error on both


I have no idea, why you're getting an error... Your code is working fine. You can test it this way:
VB
Dim result As Boolean =  numbers.Contains("on5e")
Console.WriteLine(result) 'displays "False"


If you're interested in comparison ignoring case, you can try this:
VB
Dim result As Boolean =  numbers.Contains("one", StringComparer.InvariantCultureIgnoreCase)
Console.WriteLine(result) 'displays "True"


For further details, please see: StringComparer | Microsoft Docs[^]
 
Share this answer
 
Comments
Ralf Meier 4-Jan-21 4:28am    
I suppose he gets an Error because his code isn't covered in a method ...

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

  Print Answers RSS


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