Associative array, as in php in vb.net

in PHP we know to create an associative array using this code

$variable = array('0001'=>'value1', '0010'=>'value2'); 

and print all keys and values ​​using this code

 foreach($variable as $key1 => $val1) foreach($val1 as $key2 => $val2) echo ("$key2 => $val2 <br />") 

and the question is how to do this in vb.net?

as I know, to create an associative array in vb.net using this:

 Dim var As New Collection var.Add("value1", "0001") var.Add("value2", "0010") 

how to print value and key in vb.net as foreach in php? thanks

+4
source share
1 answer

Although I am not familiar with PHP (more), I assume that associative arrays are equivalent to HashTable or more modern, strongly typed Dictionary :

 Dim dict = New Dictionary(Of String, String) dict.Add("value1", "0001") dict.Add("value2", "0010") 

Usually you should look for keys:

 Dim val2 = dict("value2") ' <-- 0010 

But if you want to list it (less efficiently):

 For Each kv As KeyValuePair(Of String, String) In dict Console.WriteLine("Key:{0} Value:{1}",kv.Key, kv.Value) Next 
+11
source

All Articles