有没有一种方法可以通过字符串引用数组元素 [英] Is there a way to refer to an array element by string
问题描述
嘿,
有谁知道用字符串引用制作数组的方法(如PHP):
$data["shopping"]["item1"] = "item name";
在vb.net中,我喜欢这样做
Hey,
Does anybody know a way to make array (like in PHP) with a string reference:
$data["shopping"]["item1"] = "item name";
In vb.net I do it like
dim data(1,1) as string<br />
data(0,0)="item name"
但是读回代码更困难...
我知道您必须在vb中使用之前定义数组大小,
但是有没有办法通过字符串引用数组元素?
(data("shopping","item1")="item name"
)
but it''s more difficult to read back the code...
I Know you have to define the array size before using in vb,
but is there a way to refer to an array element by string?
( data("shopping","item1")="item name"
)
Thanks!!
推荐答案
data ["shopping"] ["item1"] =商品名称";
在vb.net中,我喜欢这样做
data["shopping"]["item1"] = "item name";
In vb.net I do it like
dim data(1,1) as string<br />
data(0,0)="item name"
但是读回代码更困难...
我知道您必须在vb中使用之前定义数组大小,
但是有没有办法通过字符串引用数组元素?
(data("shopping","item1")="item name"
)
谢谢!!!
but it''s more difficult to read back the code...
I Know you have to define the array size before using in vb,
but is there a way to refer to an array element by string?
( data("shopping","item1")="item name"
)
Thanks!!
您最好的选择可能是双Dictionary
:
Your best bet is probably a doubleDictionary
:
Dim data As New Dictionary(Of String, Dictionary(Of String, String))
data("shopping") = New Dictionary(Of String, String)
data("shopping")("item1") = "item name"
我认为这是不可能的.但是,您可能要使用列表(T) [ ^ ]或 ^ ].它们更多是".NETty",并且更易于使用. Arrays和Lists(Of T)之间的差别很小,但是如果使用正确,我认为Arrays不能做Lists(Of T)无法完成的任何工作.当然,某些Array/List(Of T)专家现在会证明我错了...; p
编辑,我知道我的答案没有得到充分解释:
如果您使用具有唯一ID的类,则可能只需要一个List(Of T),然后可以通过LINQ查询通过其ID进行引用.
I do not think that is possible. However, you might want to use a List(Of T)[^] or Dictionary(Of T)[^]. They are a bit more ''.NETty'' and easier to use. There is a small difference between Arrays and Lists(Of T), but when used correctly I don''t think Arrays can do anything that Lists(Of T) can''t. Of course some Array/List(Of T) pro will now prove me wrong... ;p
Edit, I understood my answer was not sufficiently explained:
If you use a Class with a unique ID you would probably only need a List(Of T) which you can then reference by its ID through, perhaps a LINQ query.
Dim shoppingList As New List(Of ShoppingItem)
Dim item = (From i In shoppingList.Items Where i.ID = "item1").FirstOrDefault 'From the top of my head.
当确实需要Dictionary(Of T)时,可以将其Key用作字符串,在OP的情况下为"ItemName",并使用一个表示Item的Class.我怀疑这只是一个字符串.在大多数现实世界中的应用中,您都会拥有一个班级.
When you do need a Dictionary(Of T) you could use its Key as a string, in OP''s case "ItemName" and a Class that represents Item. I doubt it is only a String. In most real world apps you''d have a Class.
Dim shoppingDict As New Dictionary(Of String, ShoppingItem)
Dim item = shoppingDict.Item("ItemName") ' This returns the ShoppingItem Class with that item name.
如果您只需要一个包含字符串的项目,则应该看到cPallini的答案.
If you would need an item which only has a string you should see cPallini''s answer.
这篇关于有没有一种方法可以通过字符串引用数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!