mam:Sprawdź, czy lista ciągów zawiera wartość
Public lsAuthors As List(Of String)
chcę dodać wartości do tej listy, ale przed dodaniem muszę sprawdzić, czy dokładna wartość nie jest już w nim. Jak mogę to rozgryźć?
mam:Sprawdź, czy lista ciągów zawiera wartość
Public lsAuthors As List(Of String)
chcę dodać wartości do tej listy, ale przed dodaniem muszę sprawdzić, czy dokładna wartość nie jest już w nim. Jak mogę to rozgryźć?
Można użyć List.Contains
:
If Not lsAuthors.Contains(newAuthor) Then
lsAuthors.Add(newAuthor)
End If
lub z LINQs Enumerable.Any
:
Dim authors = From author In lsAuthors Where author = newAuthor
If Not authors.Any() Then
lsAuthors.Add(newAuthor)
End If
Można również użyć skutecznego HashSet(Of String)
zamiast listy, która nie zezwala na duplikaty i zwraca False
w HashSet.Add
, jeśli ciąg znaków był już w zestawie.
Dim isNew As Boolean = lsAuthors.Add(newAuthor) ' presuming lsAuthors is a HashSet(Of String)
Ogólna lista zawiera metodę o nazwie Contains, która zwraca wartość true, jeśli domyślny porównawca dla wybranego typu znajduje element spełniający kryteria wyszukiwania.
dla listy (Of String) jest to normalne porównanie ciąg, więc kod może być
Dim newAuthor = "Edgar Allan Poe"
if Not lsAuthors.Contains(newAuthor) Then
lsAuthors.Add(newAuthor)
End If
Na marginesie, porównanie domyślna dla ciągów uważa dwa ciągi inna, jeśli nie mają one w tym samym przypadku. Jeśli więc spróbujesz dodać autora o nazwie "edgar allan poe" i masz już dodaną osobę o nazwie "Edgar Allan Poe", barebone Contains nie zauważy, że są one takie same.
Jeśli trzeba zarządzać tej sytuacji musisz
....
if Not lsAuthors.Contains(newAuthor, StringComparer.CurrentCultureIgnoreCase) Then
.....
Aby sprawdzić, czy element występuje na liście, można użyć metody list.Contains(). Jeśli używasz Kliknij przycisk, aby zapełnić listę ciągów wtedy zobaczyć kod:
Public lsAuthors As List(Of String) = New List(Of String) ' Declaration of an empty list of strings
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' A button click populates the list
If Not lsAuthors.Contains(TextBox2.Text) Then ' Check whether the list contains the item that to be inserted
lsAuthors.Add(TextBox2.Text) ' If not then add the item to the list
Else
MsgBox("The item Already exist in the list") ' Else alert the user that item already exist
End If
End Sub
Uwaga: linia po linii wyjaśnienie jest podane jako komentuje
Możesz otrzymać listę pasujących elementów Twój stan w następujący sposób:
Dim lsAuthors As List(Of String)
Dim ResultData As String = lsAuthors.FirstOrDefault(Function(name) name.ToUpper().Contains(SearchFor.ToUpper()))
If ResultData <> String.Empty Then
' Item found
Else
' Item Not found
End If
Próbowałem już wcześniej, otrzymuję obiekt odniesienia nie ustawiony na wystąpienie obiektu. – Medise
@Medise: następnie zainicjuj listę. 'Publiczne lsAuthory jako nowa lista (ciągu)' –
Co za cholerny błąd zrobiłem, to działało, dziękuję. – Medise