包含字符串数组作为值的字典 [英] Dictionary containing an array of strings as value
问题描述
是否可以将字符串数组作为字典中的值?我需要将描述 (Hour2) 保存为能够访问价格 (elements_PR(4)) 和时间偏移量 (1) 的键和值.有什么好的办法吗?
Is it possible to have a string array as the value in a dictionary? I need to save the description (Hour2) as the key and as value being able to access both the price (elements_PR(4)) and the time offset (1). Is there a good way to do that?
Dim pr_val(2) As String
Dim PR As New Dictionary(Of String, pr_val)
PR.Add("Hour2", {elements_PR(4), "1"})
推荐答案
在我看来,您可以创建一个表示价格和时间偏移的类.因此,您可以执行类似 PR.Add("Hour2", instanceOfClass)
之类的操作.
It seems to me that you could create a class representing the price and the time offset. Therefore, you could do something like PR.Add("Hour2", instanceOfClass)
.
根据描述在您的情况下的含义,您甚至可以将其包含在您的班级中.它允许您使用另一种方法,其中 List(Of YourClass)
包含带有描述、价格和时间偏移的项目列表.要通过键"检索项目,您可以使用一些 Linq.
Depending on the meaning of the description in your situation, you could even include it in your class. It would allow you to use another approach with a List(Of YourClass)
containing a list of items with the description, the price and the time offset. To retrieve an item by "key", you could then use some Linq.
这篇关于包含字符串数组作为值的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!