Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding an element in a dictionary for VB.NET

I have a dictionary for some phrases and some custom short forms for them, stored in a dictionary. However, I couldn't find any methods to find a particular element in this dictionary. I want something that finds the element's (both the phrase and the code) position in the dictionary and returns the pair of elements. Is there such a thing?

Dim Codes As New Dictionary(Of String, String) From {
    {"Example 1", "E1"},
{"Example 2", "E2"},
{"Example n", "En"},
{"Not an example", "NE"},
{"Deez Nuts", "DN"},
{"MLG Noscopers", "MN"},
{"I <3 Stack Overflow", "SO"},
{"Stuff", "S"},
{"Jon Skeet is OP", "JS"},
{"Community is a bot", "CB"},
{"Jeff Atwood's icon is creepy", "JA"},
{"Meta.meta.meta.meta.meta.stackoverflow.com", "MM"},
{"Jon Skeet <> John Cena", "JC"},
{"I wanna downvote comments", "DC"},
{"not just reporting them", "NJ"},
{"ur still reading this?", "UR"},
{"go to youareanidiot.org :)", "YI"},
{"copy me", "CM"},
{"Déjà vu.", "DV"},
{"Déjà vu?", "DV_"},
{"Déjà vu!", "DV__"},
{"Let's loop MLG can can on youtube", "CC"},
{"Or Darude - Dankstorm", "DD"},
{"I am red", "IR"},
{"Imma shut up for now", "IM"}}
like image 706
Happypig375 Avatar asked Sep 14 '25 05:09

Happypig375


2 Answers

Try this

'Sub   

Public Sub HandleDictionary(ByVal Key As String)
    Dim Codes As Dictionary(Of String, String) = fnGetDictionary() ' Get Dictionary values

    If (Codes.ContainsKey(Key)) Then
        Dim v As New KeyValuePair(Of String, String)
        v = Codes.First(Function(S) S.Key.Equals(Key))
        Console.WriteLine(String.Format("KEY:{0} VALUE:{1} INDEX POSITION:{2}", Key, Codes(Key), Codes.ToList().IndexOf(v)))
    Else
        Console.WriteLine(Key + " not exists.")
    End If
End Sub

' The main method
Sub Main()
    Dim m As New myVbClass()
    m.HandleDictionay("Example 2")
    m.HandleDictionay("Example 0")
    m.HandleDictionay("Example n")
    m.HandleDictionay("Example 0")
    m.HandleDictionay("Stuff")
End Sub



' Function

Public Function HandleDictionay(ByVal Key As String) As String
    Dim Codes As Dictionary(Of String, String) = fnGetDictionary()
    If (Codes.ContainsKey(Key)) Then
        Dim v As New KeyValuePair(Of String, String)
        v = Codes.First(Function(S) S.Key.Equals(Key))
        Return String.Format("KEY:{0} VALUE:{1} INDEX POSITION:{2}", Key, Codes(Key), Codes.ToList().IndexOf(v))
    Else
        Return Key + " not exists."
    End If
End Function

     'MAIN
Sub Main()
    Dim m As New myVbClass()
    Console.WriteLine(m.HandleDictionay("Example 2"))
End Sub
like image 164
King Avatar answered Sep 15 '25 21:09

King


Create a class for the "pair of things" and use that class as value of Dictionary, where key of Dictionary will be your keyword

Public Class PairOfThing
    Public Property Phrase As String
    Public Property Key As String
End Class

Dim codes As New Dictionary(Of String, PairOfThing) From 
{
    {"E1", New PairOfThing With {.Phrase = "Example 1", .Key = "E1"}},
    {"E2", New PairOfThing With {.Phrase = "Example 2", .Key = "E2"}},
}

Dim data As PairOfThing = codes.Item("E1")

'Print phrase to console
Console.WriteLine(data.Phrase)
like image 23
Fabio Avatar answered Sep 15 '25 19:09

Fabio