Search code examples
vb.netmultidimensional-arraystrongly-typed-dataset

Strongly Typed Multidimensional Array/Collection


I want to create a strongly typed multidimensional array or collection containing the following values from a database:

  • FileName (As String)
  • FileSize (As Integer)

Requirements:

  • Accessible via index (e.g. Arr(i)(j), Arr.Row(i), etc)
  • Efficient (i.e. fast & not resource intensive)
  • Easily manipulated, added to, appended, etc.
  • .NET 3.5 compatible

Thanks for the great answers everyone. Here's what I went with... :)

Structure FileRecord
    Dim Name As String
    Dim Size As Integer

    Sub New(ByVal FileName As String, ByVal FileSize As Integer)
        Me.Name = FileName
        Me.Size = FileSize
    End Sub
    Sub New(ByVal Files() As FileRecord)
        For Each f As FileRecord In Files
            Dim fr As New FileRecord(f.Name, f.Size)
        Next
    End Sub
End Structure

Solution

  • You can't have a multidimensional array containing two separate types.

    Instead, you'd typically make a single dimensional array (or List(Of T)) containing a custom class with your data.

    In your case, you might want something like:

    Public Class FileRecord
        Public Property Name As String
        Public Property Size as Integer
    End Class
    

    Then, make a List(Of FileRecord) to hold your data. You'd then be able to access this as:

    Dim nameAtIndex = theList(i).Name
    Dim sizeAtIndex = theList(i).Size