Multi-dimensional array with variable number of elements in last dimension

T

Tad Marshall

Hi,

I'm reading about arrays in VB.NET and I seem to have a few options for my
data structure. I need a multi-dimensional array of structures, and my
first thought was

Public Structure myStr
Public part1 as Integer
Public part2 as Integer
End Structure

Dim simpleWay(2, 2, 2, 2, 2, 100) As myStr

This seems workable, but I usually won't actually have 100 of these things,
that's just the maximum I'll permit.

So, I read about jagged arrays, and I think I could do

Dim jaggedWay(2, 2, 2, 2, 2)() as myStr

except that the last dimension will grow and shrink at runtime and I can't
find any examples of code doing this or figure out how to get ReDim to do it
for me.

Another possibility is to use an array of Collections, like

Dim collectionWay(2, 2, 2, 2, 2) as Collection
' Should be a loop to create all elements, but I'll just do one ...
collectionWay(0, 0, 0, 0, 0) = New Collection
collectionWay(0, 0, 0, 0, 0,).Add(myStrInstance)

I think that I could equally well use ArrayList instead of Collection, that
would work more or less the same way as using Collections.

Is the jagged array approach even possible for me? The "simple" way has some
appeal since it uses early binding, but it may be pretty wasteful in memory.
So long as I don't run out of memory, I'm not sure how much I care. The
Collection or ArrayList approach seems both more clumsy to code and more
elegant in addressing my varying length requirements.

Any suggestions? Thanks!

Tad
 
G

Guest

You can create an ArrayList Class that does the casting for you so you don't
have to continually directcast the arraylist values. Below is an excerpt
from my code..it might give you some ideas;

Public Class List
Inherits ArrayList
Public Overloads Function add(ByVal KeyId As Long, ByVal Val As
String, Optional ByVal SortList As Boolean = False) As Integer
Dim a As listitem
a.id = KeyId : a.val = Val
Me.add(a)
If SortList Then
Me.Sort()
Return Me.IndexOf(Val)
Else
Return Me.Count - 1
End If
End Function
Default Public Shadows Property Item(ByVal index As Integer) As String
Get
Return DirectCast(Me(index), listitem).val
End Get
Set(ByVal Value As String)
DirectCast(Me(index), listitem).val = Value
End Set
End Property
Public Overloads Function IndexOf(ByVal val As String) As Integer
Dim i As Integer
While i < MyBase.Count
If Me.Item(i) = val Then Return i
i = i + 1
End While
Return -1
End Function
Public Overloads Function indexof(ByVal StartIndex As Integer, ByVal
val As String) As Integer
Dim i As Integer = StartIndex
If i >= MyBase.Count OrElse i < 0 Then Return -1
While i < MyBase.Count
If Me.Item(i) = val Then Return i
i = i + 1
End While
Return -1
End Function
Public Overloads Function Contains(ByVal val As String) As Boolean
If Me.IndexOf(val) >= 0 Then Return True Else Return False
End Function

Public Overloads Overrides Sub Sort()
Dim mycomparer As listcompare = New listcompare
MyBase.Sort(mycomparer)
End Sub

Public Class listcompare
Implements IComparer
Dim x1 As String
Dim y1 As String
Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements IComparer.Compare
If TypeOf y1 Is DBNull OrElse y1 Is Nothing Then y1 = ""
Else y1 = DirectCast(y, listitem).val
If TypeOf x1 Is DBNull OrElse y1 Is Nothing Then x1 = ""
Else x1 = DirectCast(x, listitem).val
Return New CaseInsensitiveComparer().Compare(y, x)
End Function
End Class 'listcompare
End Class
 
G

Guest

Sorry, I forgot to include the ListItem Class:

Friend Class listitem
Public id As Long
Public val As String
End Class 'listitem
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top