关联数组,如 vb.net 中的 php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13720264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:27:04  来源:igfitidea点击:

associative array like in php in vb.net

vb.net

提问by Yohanes AI

in PHP we know to make associative array using this code

在 PHP 中,我们知道使用此代码创建关联数组

$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?

问题是如何在 vb.net 中执行此操作?

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

据我所知,使用这个在 vb.net 中创建关联数组:

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

如何像 PHP 中的 foreach 一样在 vb.net 中打印值和键?谢谢

回答by Tim Schmelter

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

虽然我不熟悉 PHP(不再),但我认为关联数组相当于 aHashTable或更现代的强类型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

回答by mirzaei.sajad

    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)