2016-04-23 129 views
-1
Public Sub Add(ByVal phone As String, ByVal name As String) 
    MyBase.InnerHashtable.Add(phone, name) 
End Sub 

'--Subroutine to extract the value from a dictionary when the key is passed as input 
Public Function Item(ByVal phone As String) As String 
    Return CStr(MyBase.InnerHashtable.Item(phone)) 
End Function 

示例项:。的关键查字典(VB.NET

'--Adding key and values to the dictionary  

myDict.Add("6812036998", "Horton") 
myDict.Add("6812036999", "Olive") 

我需要帮助,在字典中的反向查找鉴于key我应该能够找到名称然而,我下面不会工作代码,这是错误的

Public Function lookupValue(ByVal name As String) 
    Return CStr(MyBase.InnerHashtable.Item(name)) 
End Function 
+0

为什么你想找到'name' 结束在'Dictionary'中,当已经给出'name'时。或者我想你想在'key'的时候找到'name'? – jamiedanq

回答

1

值不是唯一的,所以我想你将需要使用LINQ得到的,也就是说,第一个值。

Dim myValue As String = myDict.FirstOrDefault(Function(x) x.Value = "Horton").Key; 

编辑:

正如评论双方艾伦·马修和我自己,请务必添加System.Linq作为项目的参考之一,并把Imports System.Linq你写上面一行的文件中。

+0

它没有工作。说FirstOrDefault方法不存在。另外我需要生成一个函数。 –

+0

@AlanMathew更新。以前的语法是C#(我的道歉)。为了使它工作,你应该去你的项目参考,并添加System.Linq,也将输入System.Linq在您的.vb文件 – Ian

+0

之上的抱歉,但它仍然工作。 –

0

乌尔代码的标识符是关键。但是你需要价值作为标识符。在yourdictionary 试试这个 对于每一行作为keyvaluepair(字符串,字符串)如果row.value = desiredstring然后 Yourresult = row.key如果

下一页