|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Multi-dimensional array with variable number of elements in last dimensionI'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 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/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsarraylistclasstopic.asp However it has not any fixed bound inside its arrays. I hope this helps, Cor 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 Show quoteHide quote "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 > > > This is very helpful, thank you! Lots of good ideas in there ...
Tad Show quoteHide quote "Dennis" <Den***@discussions.microsoft.com> wrote in message news:87B3AF50-A01F-4047-AC59-7E9741922C1E@microsoft.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; > Sorry, I forgot to include the ListItem Class:
Friend Class listitem Public id As Long Public val As String End Class 'listitem Show quoteHide quote "Tad Marshall" wrote: > This is very helpful, thank you! Lots of good ideas in there ... > > Tad > > "Dennis" <Den***@discussions.microsoft.com> wrote in message > news:87B3AF50-A01F-4047-AC59-7E9741922C1E@microsoft.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; > > > > >
arraylist copy
Why do we need Namespace and Module? Easiest way to generate XML in VB.NET Excaping recursive functions Legacy Client <--> DotNet Listener Possible values of DTE.ActiveDocument.Kind ? How do you force a thread to run on a specific processor? Unable to start debuging on the web server Getting reference to an ErrorProvider Async Delegates polling for vb.net examples |
|||||||||||||||||||||||