Add item to an Array 2D using LINQ

Posted on


I’ve wrote a generic function to add an item to an Array 2D

This is what I have:

Private Sub Add_Item_Array_2D(ByRef Array_2D As String(,), _
                              ByVal Items As String())

    Dim tmp_array(Array_2D.GetUpperBound(0) + 1, Array_2D.GetUpperBound(1)) As String

    For x As Integer = 0 To Array_2D.GetUpperBound(0)
        tmp_array(x, 0) = Array_2D(x, 0)
        tmp_array(x, 1) = Array_2D(x, 1)

    For x As Integer = 0 To Items.Count - 1
        tmp_array(tmp_array.GetUpperBound(0), x) = Items(x)

    Array_2D = tmp_array

End Sub

The code works, but I want to know if can be improved using Array.Resize method of LINQ extensions.

This is an usage example:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.shown

    ' Create Array 2D (2,2)
    Dim MyArray As String(,) = _
    {{"Item 0,0", "Item 0,1"}, {"Item 1,0", "Item 1,1"}, {"Item 2,0", "Item 2,1"}}

    ' Add Item
    Add_Item_Array_2D(MyArray, {"Item 3,0", "Item 3,1"})

    ' Loop over the Array 2D
    For x As Integer = 0 To MyArray.GetUpperBound(0)
        MsgBox(String.Format("Array 2D {1},0: {2}{0}Array 2D {1},1: {3}", Environment.NewLine, _
                            x, MyArray(x, 0), MyArray(x, 1)))

End Sub


Couple nitpicks:

  • Try to stick to PascalCasing for method names. Using _ underscores not only makes your method names look rather messy, the underscore is conventionally used for event handler methods (like Form1_Load, i.e. object_event).
  • Consistency is king. Your two snippets aren’t consistent as far as parameter naming goes: you’re using PascalCasing in your top example, and camelCasing in the bottom one. Pick one, stick to it.
  • The term generic has a specific meaning in the .net world. That’s not a generic function. General-purpose, maybe?

A quick Google search didn’t yield much interesting results in, but this StackOverflow answer has a decent C# version of the code – and it just happens to be an actual generic method:

Most methods in the array class only work with one-dimensional arrays, so you have to perform the copy manually:

T[,] ResizeArray<T>(T[,] original, int rows, int cols)
    var newArray = new T[rows,cols];
    int minRows = Math.Min(rows, original.GetLength(0));
    int minCols = Math.Min(cols, original.GetLength(1));
    for(int i = 0; i < minRows; i++)
        for(int j = 0; j < minCols; j++)
           newArray[i, j] = original[i, j];
    return newArray;

Now I know this isn’t VB, but an array is an array regardless of the language, so I strongly recommend you take a look at the SO answer, as it goes in much more details than I’d care to quote over here.

Bottom line: You cannot use Array.Resize() with multidimensional arrays.

Bottom line: You cannot use Array.Resize() with multidimensional

On the other hand, a 2-D list eliminates the resizing altogether.List(Of List(Of String)) adding items is as easy as the following:

Private Sub Add_Item_Array_2D(ByRef Array_2D As List(Of List(Of String)), _
                              ByVal Items As String())
    Array_2D.Add(New List(Of String)(Items))

End Sub

To resize multidimentional arrays without using any lists, read the MSN documentation for Array.Resize<T>().

Leave a Reply

Your email address will not be published. Required fields are marked *