-1

I want to create 2 dimensional array(4,4).

1   5   9   13

2   6   10  14

3   7   11  15

4   8   12  16

However, when I created the code, it shows vertically, only 1 dimension.

1
2
3
4
.
.

This is my code

Public Class Form1

Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
    Dim array(3, 3) As Integer
    Dim array2 As Integer(,)
    array2 = New Integer(3, 3) {}

    array(0, 0) = "1"
    array(1, 0) = "2"
    array(2, 0) = "3"
    array(3, 0) = "4"
    array(0, 1) = "5"
    array(1, 1) = "6"
    array(2, 1) = "7"
    array(3, 1) = "8"
    array(0, 2) = "9"
    array(1, 2) = "10"
    array(2, 2) = "11"
    array(3, 2) = "12"
    array(0, 3) = "13"
    array(1, 3) = "14"
    array(2, 3) = "15"
    array(3, 3) = "16"

    lstArray.Items.Add(array(0, 0))
    lstArray.Items.Add(array(1, 0))
    lstArray.Items.Add(array(2, 0))
    lstArray.Items.Add(array(3, 0))
    lstArray.Items.Add(array(0, 1))
    lstArray.Items.Add(array(1, 1))
    lstArray.Items.Add(array(2, 1))
    lstArray.Items.Add(array(3, 1))
    lstArray.Items.Add(array(0, 2))
    lstArray.Items.Add(array(1, 2))
    lstArray.Items.Add(array(2, 2))
    lstArray.Items.Add(array(3, 2))
    lstArray.Items.Add(array(0, 3))
    lstArray.Items.Add(array(1, 3))
    lstArray.Items.Add(array(2, 3))
    lstArray.Items.Add(array(3, 3))

End Sub
End Class

How can I make this 2 dimensional array? Thanks in advance.

6
  • If you debug the application and hover over lstArray, can you see what content is contained? Your code looks to be ok, but is incomplete. How are you iterating over lstArray (which I presume is a list of Int)? Commented Jun 24, 2015 at 12:13
  • 1
    Are you asking how to make a listbox have multiple columns? Commented Jun 24, 2015 at 12:13
  • 1
    Your array is of type Integer, yet you are enclosing your values in double quotes. Commented Jun 24, 2015 at 12:15
  • Ok, for what you have shown, your code for creating the array (other than the double quotes) should work fine. Your use of the list object (lstArray) is I presume just so you can see the contents of the array. array2 doesn't need to be DIM'd if you're not using it, it just adds clutter. How are you displaying the content of the array? Commented Jun 24, 2015 at 12:21
  • I removed the double quotes. But it still doesn't work. I display by using the button. Commented Jun 24, 2015 at 12:46

1 Answer 1

1

You gave us the code for how you created the array, but not how you displayed it. I'm confident that your problem lies there (in the button_click event, or wherever your code reads the array back to you).

My guess is the unshared code has one of three problems:

1) It's only looping through 1 dimension of array. So where you have something like

For i = 0 To 3
    MsgBox(array(0, i)
Next

what you need is something like

For i = 0 To 3
    For j = 0 To 3
        MsgBox(array(i, j)
    Next
Next

2) It's referencing lstArray, but at some point confusing the structure of lstArray with that of array. Whereas array's indices range from (0,0) to (3,3), lstArray's range from (0) to (15). You could only be looping from 0 to 3 if you got your variables confused or changed from one to the other at some point in your coding.

3) There's a mistake somewhere in how you are concatenating (or otherwise combining) the separate items in lstArray into a single row.

See if you can solve or at least narrow down your problem based on these suggestions and then post the missing code if you still need help.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help. First, I will try by myself.

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.