By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,918 Members | 1,662 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,918 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Tad,

When you want a jagged array, than you can use an arraylist, be aware that
in that case you need all the time casting.

http://msdn.microsoft.com/library/de...classtopic.asp

However it has not any fixed bound inside its arrays.

I hope this helps,
Cor
Nov 21 '05 #2

P: n/a
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

"Tad Marshall" wrote:
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

Nov 21 '05 #3

P: n/a
This is very helpful, thank you! Lots of good ideas in there ...

Tad

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
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;

Nov 21 '05 #4

P: n/a
Sorry, I forgot to include the ListItem Class:

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

"Tad Marshall" wrote:
This is very helpful, thank you! Lots of good ideas in there ...

Tad

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
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;


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.