Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a for each loop in vb.net for this particular example there are 2 items in list but after the first item the loop exits are there errors in the code

Public Function findUserID(ByVal list As List(Of KeyValuePair(Of String, String)), ByVal value As String)
    Dim id As String = String.Empty
    For Each kvp  In list
        If (kvp.Value = value) Then
            id = kvp.Key
        End If
    Next
    Return id
End Function
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
156 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try to use this:

dim kvp as KeyValuePair
kvp = list.Find(p=>p.Value = value))
if kvp = null then return "" else return kvp.Key

One user told me to modify it in this way:

dim kvp = list.Find(Function(e) e.Value = value)
If kvp Is Nothing Then Return "" Else Return kvp.Key

Sorry if this code has some error, but I cannot try and I usually write in C#.
So my code (in C#) would be:

KeyValuePair kvp = list.Find(p=>p.Value == value));
return kvp == null ? "" : kvp.Key;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...