vb.net 访问字典的键/值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17817538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:21:42  来源:igfitidea点击:

Accessing the key/value of a dictionary

vb.netdictionary

提问by bbesase

How do I access the key/value of a dictionary? I have this code:

如何访问字典的键/值?我有这个代码:

    Try


        If keystrokeDictionary.ContainsKey(letter) Then
            keystrokeDictionary.Keys.Equals(letter)
        End If

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    Return letter

Letteris a single letter that gets passed in with just its string value. Then keystrokedictionaryis a dictionary that the information looks like this:

Letter是一个仅带有字符串值的单个字母。然后keystrokedictionary是一个字典,里面的信息是这样的:

{[65, 0]}
{[66, 1]}
{[67, 2]}

Where the key is the first number and the value is the second number. Since my code isn't looking at the key/value right, it's not returning a letter, and therefore not working.

其中键是第一个数字,值是第二个数字。由于我的代码没有正确查看键/值,因此它没有返回字母,因此无法正常工作。

回答by Curt

.Equals()is not an assignment operator, but a comparison function.

.Equals()不是赋值运算符,而是比较函数。

Try

尝试

letter = " ";

If keystrokeDictionary.ContainsKey(letter) Then
   letter = keystrokeDictionary.Values(letter)
End If

Return letter

回答by Sivashankar

Try like this,Use item to get corresponding value

试试这样,使用 item 来获取相应的值

letter = " ";

If keystrokeDictionary.ContainsKey(letter) Then
letter = keystrokeDictionary.Item(letter)
End If

Return letter