Happypig375 Happypig375 - 5 months ago 16
Vb.net Question

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"}}

Answer

Just loop it without Select Cases, no nested classes and single function (Finally figured it out):

Friend Function ConvertCode(Input As String, ToCode As Boolean) As String
    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"}}
    For Each Pair As KeyValuePair(Of String, String) In Codes
        If ToCode Then
            If Pair.Key = Input Then Return Pair.Value
        Else
            If Pair.Value = Input Then Return Pair.Key
        End If
    Next
    Throw New KeyNotFoundException("Input is unconvertable.")
End Function
Comments