1

I am trying to create an array in visual basic to store multiple values, i want to do it like in PHP:

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

then looping through:

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

}

what would be the equivalent in VB.net?

3
  • The docs (msdn.microsoft.com/en-us/library/…) give a clue: Dim values As Double() = {1, 2, 3, 4, 5, 6} Commented Aug 5, 2016 at 10:14
  • i want it to be multidimensional like you can do in PHP so i can store 2 values for each array object Commented Aug 5, 2016 at 10:16
  • Dim a = {{1, 2.0}, {3, 4}, {5, 6}, {7, 8}} except you might want a Dictionary if you want to do lookup Commented Aug 5, 2016 at 10:25

1 Answer 1

2

For a PHP array like:

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

The VB.Net equivalent is a Dictionary(Of String, String).

Two examples of usage:

    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

This example uses a collection initializer and is a bit closer to your 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
Sign up to request clarification or add additional context in comments.

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.