how to make array of arrays?

S

Steve

Hello,

I created a structure ABC and an array of type ABC

Public Structure ABC
Dim str1 As String
Dim int1 As Integer
End Structure

Public ABC1 As New ABC, ABC2 As New ABC
Public ABC3 As New ABC, ABC4 As New ABC
Public arrABC1() As ABC = {ABC1, ABC2}'--this array OK
Public arrABC2() As ABC = {ABC3, ABC4}'--this array OK

Now I want to place arrABC1 and arrABC2 into an array.
How to do this? I tried

Public arrABC() As ABC = {arrABC1(1), arrABC2(1)}'--ok

and call it like this

....arrABC(0)(0) to access structure ABC1 -- Not OK here

but I get an error message that I can't do this because
Structure ABC has no default property (option strict on -
must remain on). I could either add a default property to
the structure (how do you do that?) or I could use a
different kind of array type object. I have tried
ArrayList, but that did not work. I tried an Object()
array, but that did not work. Any suggestions appreciated.

Thanks,
Steve
 
J

Jay B. Harlow [MVP - Outlook]

Steve,
Public arrABC() As ABC = {arrABC1(1), arrABC2(1)}'--ok
No! you defined an array of ABC, not an array of array of ABC. arrABC has
ABC1 & ABC3 in it.

Try:

Public arrABC()() As ABC = {arrABC1, arrABC2}

Then will work:
Dim a As ABC = arrABC(0)(0)

Hope this helps
Jay
 
S

Steve

Yes! Thank you very much. I just figured that out before
I got to the post. You concur!

Thanks for your reply.
Steve
 
J

Jay B. Harlow [MVP - Outlook]

Doh!
No! you defined an array of ABC, not an array of array of ABC. arrABC has
ABC1 & ABC3 in it.
Damn based 0 indexes ;-) Your arrABC has ABC2 & ABC4 in it...

Glad you got it to work.

Hope this helps
Jay
 
C

Cor Ligthert

Steve,

In addition to Jay,

A very easy way of making an array of arrays is using the arraylist.

And than use the directcast when you are using it.

When you want a example of that tell that.

Cor
 
S

Steve

Thanks. Yes. How would you cast this using Arraylists?

I tried arraylist with no luck

Dim arrL() As New ArrayList
arrl(1) = CType(ABC1(x), ABC)
arrl(2) = CType(ABC2(x), ABC)
....

something like that? Is there a benefit to using
ArrayLists over just creating an Array of Type ABC?
 
C

Cor Ligthert

Steve,

Here 2 samples of me, The first is arraylist array, where is some
explaination the second an arraylist arraylist

When the array is static, than you can (should) use the standard Array, when
the array is dynamic you should in my opinion never take the standard Array

I hope they help?

Cor

\\\arraylist array
Option Strict On
Public Module Main
' Sample of an arraylist that itself contains 10 classic arrays.
Public Sub Main()
Dim x As New ArrayList
Dim y() As Integer = {1, 2, 3, 4}
For i As Integer = 0 To 9
x.Add(y)
Next
MessageBox.Show(DirectCast(x(9), IList)(2).ToString)
'With option strict off you do not
'have to use the directcast and than it is
'MessageBox.Show(a(2)(2).ToString) 'but I would not do that
'I show this above to make it more classic looking for you.
'The Insert
Dim yN() As Integer = {5, 6, 7, 8}
x.Insert(1, yN)
MessageBox.Show(DirectCast(x(1), IList)(2).ToString)
End Sub
End Module
///
\\\arraylist arraylist
Dim x As New ArrayList
For i As Integer = 0 To 9
Dim y As New ArrayList
For j As Integer = 0 To 4
y.Add(Chr(j + 65))
Next
x.Add(y)
Next
MessageBox.Show(DirectCast(x(2), ArrayList)(2).ToString)
Dim yN As New ArrayList
For j As Integer = 0 To 4
yN.Add(Chr(j + 75))
Next
x.Insert(1, yN)
MessageBox.Show(DirectCast(x(1), ArrayList)(2).ToString)
///
 
S

Steve

Hi Cor,

Thanks for the example. So, to get this straight, if I do

ArrayList.Add(y)

that adds an element sequentially,

and

ArrayList.Insert(n, m)

adds an element at a selected index n?
 
C

Cor Ligthert

Steve,

Try this maybe this is easier to understand.
However every row (arraylist) can be of a different lenght keep that in mind
\\\
Dim x As New ArrayList
For i As Integer = 0 To 9
Dim y As New ArrayList
For j As Integer = 0 To 4
y.Add("")
Next
x.Add(y)
Next
DirectCast(x(2), ArrayList)(2) = "Steve"
MessageBox.Show(DirectCast(x(2), ArrayList)(2).ToString)
////

I hope this helps?

Cor
 
S

Steve

Yes. Thank you. I think I'm getting it now. It is the
DirectCast part that I was missing before. Now I got it.

Thanks.
 

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