473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
4 2578
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.