Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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"


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!!
Posted

Your best bet is probably a double Dictionary:
VB
Dim data As New Dictionary(Of String, Dictionary(Of String, String))

data("shopping") = New Dictionary(Of String, String)
data("shopping")("item1") = "item name"
 
Share this answer
 
v3
Comments
[no name] 19-May-11 8:54am    
Good. My 5 too.
CPallini 19-May-11 8:55am    
Thank you.
Sergey Alexandrovich Kryukov 19-May-11 16:52pm    
Correct, my 5. Please see my answer as well (probably it is redundant as your sample immediately works).
--SA
Sander Rossel 19-May-11 18:01pm    
Nice answer. I understood that my answer was not sufficiently explained. I have improved it too.
My 5 for this.
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.
VB
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.

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.
VB
Dim shoppingDict As New Dictionary(Of String, ShoppingItem)
Dim item = shoppingDict.Item("ItemName") ' This returns the ShoppingItem Class with that item name.



If you would need an item which only has a string you should see cPallini's answer.
 
Share this answer
 
v2
Comments
fjdiewornncalwe 19-May-11 10:31am    
OP's answer moved to comment:
Tanks!! That's exactly what I was looking for!!
Sergey Alexandrovich Kryukov 19-May-11 16:50pm    
Sorry, you failed to sat the truth. I just have to vote 1 in this case. This is QUITE POSSIBLE to implement. I advise to read my answer and remove yours.
Of course, if you think I miss something, tell me about it, I'll fix myself. But please keep in mind that I would be able to write sample implementation perfectly matching what OP wants (in C#, but VB.NET is perfectly the same, slightly different syntax).
--SA
Sander Rossel 19-May-11 17:59pm    
The OP's question was "Is there a way to refer to an array element by string?" and I think we all agree that is not possible. As I said in my answer.
Using a Dictionary(Of T) is what I proposed instead.

I never said the OP should not use a Dictionary in a Dictionary, but if the OP is using Arrays then perhaps he does not know about Generics. Slapping him in the face with Dictionary(Of String, Dictionary(Of String, String)) might be a bit overwhelming at first. And getting the basics is, in my opinion, more important then getting a fully worked out example :)
I think you got downvoted because its to complex (don't you hate it when people downvote, but don't mention why...).
cPallini's answer is perfectly fine and quite in line with my answer. Just a bit more worked out.
I have edited my solution so it might be a bit more clear why I did not use a double Dictionary.
I hope you would reconsider your vote :)
Sergey Alexandrovich Kryukov 19-May-11 16:53pm    
Oh, not need to write anything: C Pallini did that, please check his answer.
--SA
Simon_Whale 19-May-11 18:27pm    
Nice answer +5 It shows that not all methodologies are transferable but there are always alternatives to use
This is possible using this indexer.
For some example, please see my code sample:
Location of a refernced object in an array?[^].

Please locate my second answer using Neuron this[].

The index can be anything, including the string. In your case, you need to have an instance of System.Collections.Generic.Dictionary behind. It will give you exact same semantics as with PHP. Double indexing you show is done like this: fist index returns some object using this indexer of string as well.

—SA
 
Share this answer
 
Comments
CPallini 20-May-11 3:10am    
Downvote balanced. :-)
Sergey Alexandrovich Kryukov 20-May-11 3:19am    
Well, thank you very much. Have no idea why such a low vote. However, my answer was redundant next to yours which was quite good.
--SA
CPallini 20-May-11 3:23am    
Your Neuron class looks very interesting but beyond my current C# knowledge.
Sergey Alexandrovich Kryukov 20-May-11 23:26pm    
Thank you.
I can't believe that. There is just two syntax ideas in that code: 1) indexers, 2) more than one indexer per class through the use of interface. One short paragraph in language description.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900