2016-06-09 22 views
1

我有一些詞組和一些自定義短表單,存儲在字典中。但是,我找不到任何方法來查找此字典中的特定元素。我想要的東西可以在字典中找到元素的(包括短語和代碼)的位置,並返回元素對。有這樣的事嗎?在VB.NET中查找字典中的元素

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

您的'詞典'有三次作爲密鑰的'Déjàvu?',這是無效的,因爲這些密鑰應該是唯一的。也許你的意思是用短格式作爲關鍵詞,而短語作爲價值觀? –

+0

@Robin糟糕...編輯了代碼。 – Happypig375

+0

你爲什麼不簡單地循環鍵(或值)? – genespos

回答

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 
2

創建一個類的「對事」,並使用該類作爲字典的值,其中字典的關鍵將是您的關鍵字

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) 
+0

我也可以從代碼中得到這個短語嗎? – Happypig375

+0

是的,通過調用'data.Phrase'屬性 – Fabio

+0

等待,我的意思是代碼從短語:) – Happypig375

2

試試這個

'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 
+0

查找索引位置使用此Codes.ToList()。IndexOf(v) – King

+0

您可以使它成爲一個函數返回索引,而不是顯示在控制檯?我應該如何訪問子程序之外的代碼? – Happypig375

+0

作爲函數(字符串)更改子例程並返回值。 – King