简体   繁体   English

在VB.NET中使用键显示集合数据

[英]Displaying collection data with key in VB.NET

I have made Collection with keys but I am unable to retrieve this information. 我用密钥创建了Collection,但我无法检索这些信息。 This is my code: 这是我的代码:

Dim test As Collection = New Collection
test.Add("This is a test", "testkey")
MsgBox(test("testkey").ToString)

I'm getting an error on last line: 我在最后一行收到错误:

Argument 'Index' is not a valid value. 参数“索引”不是有效值。

What's wrong? 怎么了?

If you want to store key/value pairs use, Dictionary . 如果要存储键/值对,请使用Dictionary Here is how you can do it. 这是你如何做到的。

Dim dictionary As New Dictionary(Of String, String)
dictionary.Add("testkey", "This is a test")
MsgBox(dictionary("testkey"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM