在VB.NET的字典中查找元素

时间:2016-06-09 02:21:14

标签: vb.net dictionary

我有一些短语和一些自定义简短形式的字典,存储在字典中。但是,我找不到任何方法来查找此词典中的特定元素。我想要在字典中找到元素(短语和代码)位置并返回元素对的东西。有这样的事吗?

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

3 个答案:

答案 0 :(得分:3)

试试这个

'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

答案 1 :(得分:2)

为&#34;对事物创建一个类&#34;并使用该类作为Dictionary的值,其中Dictionary的键将是您的关键字

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)

答案 2 :(得分:1)

在没有Select Case s的情况下循环它,没有嵌套类和单个函数(最终想出来):

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