5

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

0

2 Answers 2

14

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
Sign up to request clarification or add additional context in comments.

Comments

0
    Dim row As Dictionary(Of String, Object)
    Dim rows As Dictionary(Of String, Object)
    row = New Dictionary(Of String, Object)
    rows = New Dictionary(Of String, Object)

    row.Add("a", 11)
    row.Add("b", 22)
    rows.Add("ab", row)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.