2

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
Chiramisu
  • 4,687
  • 7
  • 47
  • 77
  • In light of comments I opted to go with a `Structure` instead of a class, as it has a cleaner implementation than a `Class` with `Properties`. I'll post a sample below my original question. – Chiramisu Jun 09 '12 at 00:25
  • 1
    [Dont use a mutable structure!](http://stackoverflow.com/q/441309) – MarkJ Jun 09 '12 at 19:21
  • O.o Excellent piece of knowledge. You learn something everyday. Thanks @MarkJ! :) Now are Structures mutable by definition? Or can they be made immutable? I'm a big fan of clean code. :) – Chiramisu Jun 10 '12 at 04:25

4 Answers4

4

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
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • O.o now that's a scrumptious answer. Worked until 3am, so a little brain fried atm, thanks! ;) – Chiramisu Jun 08 '12 at 18:24
  • Hello @ReedCopsey, would you mind terribly if I gave the rep to Kapil as he has less rep and his answer was quite similar and helpful? – Chiramisu Jun 09 '12 at 00:23
  • @Chiramisu Accept whichever answer you prefer ;) It's fine with me - in general, pick the answer you feel helped you the most or is best, regardless of reputation (which really doesn't matter in the grand scheme of things) – Reed Copsey Jun 09 '12 at 00:26
  • You're right, though I would like enough rep to add tags :P... I did like your example on accessing the items though, which was helpful. Thanks. ;) – Chiramisu Jun 09 '12 at 00:33
  • @Chiramisu I'm sure it'll come quick - just keep asking, and more importantly, answering questions ;) (BTW - I voted Kapil's answer up, too - and would be find if you pick his) – Reed Copsey Jun 09 '12 at 00:35
3

You can use generic list collection:

Public Class MyItem   
  Public Property  FileName As String
  Public Property  FileSize As Integer   
End Class

Now you can have a list:

Dim MyItems AS List(Of MyItem)
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
1

Keep in mind that while it's not strongly-typed, the HashTable class performs the best for storing and retrieving large amounts of data. If FileName is unique, that would be your key. Furthermore, you could wrap a hashtable in another class, and provide strongly-typed methods for adding and retrieving.

According to an answer to this SO post, Dictionary(of T) provides a generic implementation of a hashtable. Might be worth looking into.

Community
  • 1
  • 1
ssis_ssiSucks
  • 1,476
  • 1
  • 12
  • 11
0

Let's keep it SUPER SIMPLE. I really dislike when programmers make things more complicated than they need to be.

Dim Multi_Dimensional_Array As Object(,) = _
        {{"Item 1", "1", "2", "3", "4", "5"}, 
         {"Item 2", "6", "7", "8", 9"", "No More Items"}}

You can change it from "Object" to "String" if you choose.