Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

associative array like in php in vb.net

Tags:

vb.net

in PHP we know to make associative array using this code

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

and to 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 perform this in vb.net?

as i know to make associative array in vb.net using this :

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

how about to print value and key in vb.net like foreach in PHP? thanks

like image 941
Yohanim Avatar asked Dec 05 '12 09:12

Yohanim


1 Answers

Although i'm not familiar with PHP (anymore), i assume that associative arrays are the equivalent of a HashTable or the more modern, strongly typed Dictionary:

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

Normally you would lookup keys:

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

But if you want to enumerate it (less efficient):

For Each kv As KeyValuePair(Of String, String) In dict
    Console.WriteLine("Key:{0} Value:{1}",kv.Key, kv.Value)
Next
like image 163
Tim Schmelter Avatar answered Sep 26 '22 20:09

Tim Schmelter