Search code examples
arraysvb.netmultidimensional-array

VB .NET list of 2D array of structure


I need to store in a 1D array some 2D arrays of a structure, that is:

Public Structure CodeBlock
    Public R As Integer
    Public C As Integer
    Public text_tx As String
    Public text_bx As String
    Public selected As Boolean
End Structure

Public code(nrows, ncols) As CodeBlock

Public history(5) As List(Of code) '<-- the problem

Solution

  • In List(Of code), code is supposed to be a type name; however, it is a variable. Since you don't have a type name for the 2d array, combine the two declarations into one.

    Public history As List(Of CodeBlock(,)) ' List of 2D array of CodeBlock
    

    Note that this is an empty list. You will have to add the elements like this:

    history.Add(New CodeBlock(nRows - 1, nCols - 1) {})
    history(0)(0) = ...
    history(0)(1) = ...
    

    Or assign the 2D array to a variable first

    Dim code = New CodeBlock(nRows - 1, nCols - 1) {}
    history.Add(code)
    code(0) = ...
    code(1) = ...