使用多个值创建相当于PHP数组的Vb.net

时间:2016-08-05 10:09:40

标签: vb.net

我正在尝试在visual basic中创建一个存储多个值的数组,我想在PHP中这样做:

$arr = array("1" => "one", "2" => "two");

然后循环:

foreach($arr as $a => $b) {

}

VB.net中的等价物是什么?

1 个答案:

答案 0 :(得分:2)

对于像以下的PHP数组:

$arr = array("1" => "one", "2" => "two");

VB.Net等价物是Dictionary(Of String, String)

两个使用示例:

    Dim dicA As New Dictionary(Of String, String)
    dicA.Add("1", "one")
    dicA.Add("2", "two")
    For Each Item As String In dicA.Values
        Console.WriteLine(String.Format("{0}", Item))
    Next

此示例使用集合初始值设定项,并且更接近您的PHP:

    Dim dicB As New Dictionary(Of String, String) From {{"1", "one"}, {"2", "two"}}
    For Each Item As String In dicB.Values
        Console.WriteLine(String.Format("{0}", Item))
    Next