<p>This has been driving me crazy for a few days.  Why doesn't the following work?</p>

<blockquote>
<pre><code>    Dim arr(3, 3) As Integer

    For y As Integer = 0 To arr.GetLength(0) - 1
        For x As Integer = 0 To arr.GetLength(y) - 1
            arr(y, x) = y + x
        Next
    Next
</code></pre>
</blockquote>

<p>Also, what if the array looked like this instead?</p>

<pre><code>{ {1, 2, 3},
  {4, 5, 6, 7, 8, 9, 9, 9},
  {5, 4, 3, 2}
}
</code></pre>

[{'answer_id': 50454, 'author': 'harpo', 'author_id': 4525, 'author_profile': 'https://Stackoverflow.com/users/4525', 'pm_score': 2, 'selected': False, 'text': '<p><code>arr.GetLength(y)</code></p>\n\n<p>should be</p>\n\n<p><code>arr.GetLength(1)</code></p>\n'}, {'answer_id': 50459, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 3, 'selected': False, 'text': "<p>Because there is no '2' or '3' dimension.  Should be .GetLength(1) instead of .GetLength(y)</p>\n\n<p>Also:  in VB.Net array declarations work a little differently.  The subscript you specify in the declaration is the last index, not the number of items created like with C# or C++.   But the array is still 0-indexed like C# or C++, instead of 1-indexed like VB6.  That means that if you move to VB.Net from a different language your array instincts are probably wrong, no matter which language it is.  In VB.Net, <em>Dim arr(3,3) As Integer</em> actually creates a <strong>4x4</strong> array. </p>\n"}, {'answer_id': 50508, 'author': 'Akira', 'author_id': 795, 'author_profile': 'https://Stackoverflow.com/users/795', 'pm_score': 1, 'selected': False, 'text': '<p>Well what if I had an array that looked like this</p>\n\n<pre><code>{ {1, 2, 3},\n  {4, 5, 6, 7, 8, 9, 9, 9},\n  {5, 4, 3, 2}\n}\n</code></pre>\n\n<p>How would GetLength(1) still know the length of each row?</p>\n\n<hr>\n\n<p>Basically what I want is.... <strong>a way to find the number of elements in any given row</strong>.  </p>\n'}, {'answer_id': 50569, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': -1, 'selected': False, 'text': "<p>Your declaration: <code>DIM arr(3,3) As Integer</code> allready specifies that there are 3 elements in any given row (or 4, I'm not so sure about VB)</p>\n\n<p>You could try:</p>\n\n<pre><code>Dim arr(3) as Integer()\n</code></pre>\n\n<p>You should then be able to do:</p>\n\n<pre><code>arr(n).Length\n</code></pre>\n\n<p>To find the length of row n.</p>\n\n<p>I'm a bit rusty on VB6 and never learned VB.NET, but this should give you a 'jagged' array. Check out the msdn documentation on multidimensioned arrays.</p>\n"}, {'answer_id': 50577, 'author': 'osp70', 'author_id': 2357, 'author_profile': 'https://Stackoverflow.com/users/2357', 'pm_score': 0, 'selected': False, 'text': "<pre><code>Dim arr(3, 3) As Integer\nDim y As Integer\nDim x As Integer\n\nFor x = 0 To arr.Rank - 1\n    For y = 0 To arr.GetLength(x) - 2\n        arr(x, y) = x + y\n    Next\nNext\n</code></pre>\n\n<p>The above code worked for me. </p>\n\n<p>Edit, the code feels dirty though.  I'm wondering what it is you are trying to accomplish?  </p>\n"}, {'answer_id': 50633, 'author': 'Jonathan Allen', 'author_id': 5274, 'author_profile': 'https://Stackoverflow.com/users/5274', 'pm_score': 4, 'selected': True, 'text': '<p>Ok, so what you really need is a "jagged array". This will allow you to have an "array that contains other arrays of varying lengths".</p>\n\n<pre><code>  Dim arr As Integer()() = {New Integer() {1, 2, 3}, New Integer() {4, 5, 6, 7, 8, 9, 9, 9}, New Integer() {5, 4, 3, 2}}\n\n  For x = 0 To arr.GetUpperBound(0)\n      Console.WriteLine("Row " &amp; x &amp; " has " &amp; arr(x).GetUpperBound(0) &amp; " columns")\n      For y = 0 To arr(x).GetUpperBound(0)\n          Console.WriteLine("(" &amp; x &amp; "," &amp; y &amp; ") = " &amp; arr(x)(y))\n      Next\n   Next\n</code></pre>\n\n<p>Output: </p>\n\n<pre><code>Row 0 has 2 columns\n(0,0) = 1\n(0,1) = 2\n(0,2) = 3\nRow 1 has 7 columns\n(1,0) = 4\n(1,1) = 5\n(1,2) = 6\n(1,3) = 7\n(1,4) = 8\n(1,5) = 9\n(1,6) = 9\n(1,7) = 9\nRow 2 has 3 columns\n(2,0) = 5\n(2,1) = 4\n(2,2) = 3\n(2,3) = 2\n</code></pre>\n'}, {'answer_id': 1370352, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>This code en C# is to get all the combinations of items in a jagged array:</p>\n\n<pre><code>    static void Main(string[] args)\n    {\n        bool exit = false;\n        int[] indices = new int[3] { 0, 0, 0 };\n        string[][] vectores = new string[3][];\n\n        vectores[0] = new string[] { "A", "B", "C" };\n        vectores[1] = new string[] { "A", "B" };\n        vectores[2] = new string[] { "B", "D", "E", "F" };\n\n        string[] item;\n        int[] tamaños = new int[3]{vectores[0].GetUpperBound(0), \n            vectores[1].GetUpperBound(0), \n            vectores[2].GetUpperBound(0)};\n\n        while (!exit)\n        {\n            item = new string[]{ vectores[0][indices[0]],\n                    vectores[1][indices[1]],\n                    vectores[2][indices[2]]};\n\n            Console.WriteLine("[{0},{1},{2}]={3}{4}{5}", indices[0], indices[1], indices[2], item[0], item[1], item[2]);\n            GetVector(tamaños, ref indices, ref exit);\n        }\n        Console.ReadKey();\n    }\n\n    public static void GetVector(int[] tamaños, ref int[] indices, ref bool exit)\n    {\n        for (int i = tamaños.GetUpperBound(0); i &gt;= 0; i--)\n        {\n            if (tamaños[i] &gt; indices[i])\n            {\n                indices[i]++;\n                break;\n            }\n            else\n            {\n                //ULTIMO ITEM EN EL ARRAY, VALIDAR LAS OTRAS DIMENSIONES SI YA ESTA EN EL ULTIMO ITEM\n                if (!ValidateIndexes(tamaños, indices))\n                    indices[i] = 0;\n                else\n                {\n                    exit = true;\n                    break;\n                }\n            }\n        }\n    }\n\n    public static bool ValidateIndexes(int[] tamaños, int[] indices)\n    {\n        for (int i = 0; i &lt; tamaños.Length; i++)\n        {\n            if (tamaños[i] != indices[i])\n                return false;\n        }\n        return true;\n    }\n</code></pre>\n\n<p>The output looks like\n[0,0,0]=AAB\n[0,0,1]=AAD\n[0,0,2]=AAE\n[0,0,3]=AAF\n[0,1,0]=ABB\n[0,1,1]=ABD\n[0,1,2]=ABE\n[0,1,3]=ABF\n[1,0,0]=BAB\n[1,0,1]=BAD\n[1,0,2]=BAE\n[1,0,3]=BAF\n[1,1,0]=BBB\n[1,1,1]=BBD\n[1,1,2]=BBE\n[1,1,3]=BBF\n[2,0,0]=CAB\n[2,0,1]=CAD\n[2,0,2]=CAE\n[2,0,3]=CAF\n[2,1,0]=CBB\n[2,1,1]=CBD\n[2,1,2]=CBE\n[2,1,3]=CBF</p>\n'}]
['https://Stackoverflow.com/questions/50450', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/795/']