Results 1 to 17 of 17

Thread: Array of Arrays.... ?????

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Array of Arrays.... ?????

    I have an array of arrays... not a mult-dimensional array. I want to be able to dynamically redimension each array, but VB doesn't like how I'm doing it. I think it's not possible, but if anyone knows how, it would save me from rewriting a lot of code.

    I want to be able to do this:

    Like this:

    arr(9)(12)

    Not like this:

    arr(9, 12)

    VB Code:
    1. Private arr() As Variant
    2.  
    3. Sub AddArrayDimension(ArrayIndex As Long)
    4.  
    5. Redim Preserve Array(ArrayIndex)(Ubound(Array(ArrayIndex)) + 1)
    6.  
    7. End Sub

    Anyone have any good ideas about how to do this specifically?

  2. #2
    WorkHorse
    Guest
    How do you have an array of arrays? I think you can only do this with a user define type. you would have something like
    VB Code:
    1. ReDim arr(9)
    2. ReDim arr(9).arr(12)
    Which is sort of like a multi dimensional array anyway.

  3. #3
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Don't think you can do it like that cafeen. It'll give syntax errors in the compiler. A 2-D array is an array of arrays, effectively.

    I'm guessing the whole reason for this is so you can ReDim all bounds?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  4. #4

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    No, you can have an array of arrays. I just don't think you can have a dynamic array. I looked in MSDN and it talked about this, but it didn't say anything one way or the other about making them dynamic.

    The point to having an array of arrays as opposed to having a multidimensional array is that each array can have a different subscript. In a multi-dimensional array, each array has the same subscript.

    The main problem here is that I'm trying to do something that I really shouldn't be doing.

  5. #5
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Ah yes, I see. So something like:

    arr(2)(3)
    arr(2)(1)
    arr(2)(5)
    arr(2)(3)

    ?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  6. #6

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by rjlohan
    Don't think you can do it like that cafeen. It'll give syntax errors in the compiler. A 2-D array is an array of arrays, effectively.

    I'm guessing the whole reason for this is so you can ReDim all bounds?
    Yep... that's basically it except I really just want to redim the last bound for each array. None of the arrays would be the same size.

    I'm trying to make an array of "tables" instead of linking tables like I should be.

  7. #7

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by rjlohan
    Ah yes, I see. So something like:

    arr(0)(3)
    arr(1)(1)
    arr(2)(5)
    arr(3)(3)

    ?
    You got it. Except the first subscript is different.

  8. #8
    WorkHorse
    Guest
    If the second value is a variable, I think you have to redim the whole variable.
    VB Code:
    1. Dim F()
    2. ReDim F(10)
    3. Dim G(10)
    4. F(3) = 3
    5. G(5) = F
    6.  
    7. MsgBox G(5)(3)
    8.  
    9. ReDim Preserve F(20)
    10.  
    11. F(18) = 18
    12. G(5) = F
    13.  
    14. MsgBox G(5)(18)
    15. MsgBox G(5)(3)
    I don't thin you can make say the 5th item of G dimensioned for 20 items, but have the 6th value of G dimesioned for 10 values. I think the second value has to always have the same dimension. But I didn't even know you could do this, so I'm totally guessing.

  9. #9

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    From MSDN

    Arrays that Contain Other Arrays
    It's possible to create a Variant array, and populate it with other arrays of different data types. The following code creates two arrays, one containing integers and the other strings. It then declares a third Variant array and populates it with the integer and string arrays.
    VB Code:
    1. Private Sub Command1_Click()
    2.    Dim intX As Integer   ' Declare counter variable.
    3.    ' Declare and populate an integer array.
    4.    Dim countersA(5) As Integer
    5.       For intX = 0 To 4
    6.          countersA(intX) = 5
    7.       Next intX
    8.    ' Declare and populate a string array.
    9.       Dim countersB(5) As String
    10.          For intX = 0 To 4
    11.             countersB(intX) = "hello"
    12.          Next intX
    13.    Dim arrX(2) As Variant   ' Declare a new two-member
    14.                            ' array.
    15.       arrX(1) = countersA()   ' Populate the array with
    16.                               ' other arrays.
    17.       arrX(2) = countersB()
    18.       MsgBox arrX(1)(2)   ' Display a member of each
    19.                         ' array.
    20.       MsgBox arrX(2)(3)
    21. End Sub

    Multidimensional Arrays

    Sometimes you need to keep track of related information in an array. For example, to keep track of each pixel on your computer screen, you need to refer to its X and Y coordinates. This can be done using a multidimensional array to store the values.

    With Visual Basic, you can declare arrays of multiple dimensions. For example, the following statement declares a two-dimensional 10-by-10 array within a procedure:

    Static MatrixA(9, 9) As Double

    Either or both dimensions can be declared with explicit lower bounds:

    Static MatrixA(1 To 10, 1 To 10) As Double

    You can extend this to more than two dimensions. For example:

    Dim MultiD(3, 1 To 10, 1 To 15)

    This declaration creates an array that has three dimensions with sizes 4 by 10 by 15. The total number of elements is the product of these three dimensions, or 600.

    Note When you start adding dimensions to an array, the total storage needed by the array increases dramatically, so use multidimensional arrays with care. Be especially careful with Variant arrays, because they are larger than other data types.

  10. #10
    WorkHorse
    Guest
    So, did that answer you question? Don't redim a combo of the 2 indexes. Just Redim Preserve the variable that equates to the second index.

  11. #11

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Yeah... I haven't really played with it much. Here's the ultimate problem.

    I have a Table class that has uses a field class. The field classes can either be normal variables or arrays.

    Each field has several properties. One of the properties is whether or not the field is an array. If it's an array, it gets handled in a completely different fashion than the other fields for obvious reasons.

    So I need to be able to read a file, redimension the first dimension to the number of array fields I have and then redimension each field array to however much data it contains.

    If I can get this to work, I can add lists on the fly in an array and index them to the arrays. The same goes for the command buttons to add, edit or delete items from the list.

    The downside is that it's a flat file scheme. For example, if a record has actors in a movie, then it's not a lookup list. You have to enter the actors for each record and there's no good way to pull out all the movies with a certain actor. So basically, my whole scheme was ill conceived to begin with. I was just being lazy because the right way is another week of coding at least.

    I'll see if I can make this work easily, but the code goes something like this:

    VB Code:
    1. ' form
    2. Sub AddArrayValue(ArrayIndex As Long, Value As Variant)
    3.  
    4. ' validate here
    5.  
    6. TBL.AddArrayValue ArrayIndex, Value
    7.  
    8. End Sub
    9.  
    10. ' cTable
    11.  
    12. Public Sub AddArrayValue(ArrayIndex As Long, Value as Variant, Optional Position As Long)
    13.  
    14. With m_Array(ArrayIndex)
    15. ' need to add a subscript here
    16. End With
    I had the entire code base written and when it came time to redimension the array, the whole thing bottomed out. I ended up just closing the project without saving any of it.

  12. #12
    WorkHorse
    Guest
    You can tell whether an item in the array is an array or not using the IsArray function. I just tried it on my simple test scheme and it worked just fine to tell whether a particular item in the array holds an array or just a single value.

  13. #13

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by WorkHorse
    You can tell whether an item in the array is an array or not using the IsArray function. I just tried it on my simple test scheme and it worked just fine to tell whether a particular item in the array holds an array or just a single value.
    I know. I'm not having a problem determing if something is an array. Like I said, I'm just going about the problem in the wrong way. It shouldn't be an array to begin with. What I'm attempting to do is the equivalent of Microsoft Access storing an entire table in a single field of another table.

    Anyway... it's not a problem I'm trying to solve any more. Even if I get it working, it's going to create a lot of other problems like I outlined in my last post. The code I have now is solid, so when I want to add this functionality, I'll do it the right way. I wrote my code to be extendable and could turn it into a full-fledged database if that's what I wanted to do.

    Thanks for the help though.

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Talking

    http://www.vbforums.com/showthread.p...hreadid=185414

    This thing was 15 pages in, not easy to find.

  15. #15

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by Edneeis
    http://www.vbforums.com/showthread.p...hreadid=185414

    This thing was 15 pages in, not easy to find.
    HA! Now I remember! Woo Hoo! You know that was so I can add fields dynamically at run time with VB DB.

    Damn! I really don't feel like working on that right now. But that was the missing link. Thanks!

  16. #16

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I take it back. It wasn't the missing link from adding fields. It was allowing a table to have multiple array fields and reference them as

    array (ArrayIndex)(ItemIndex)

    or something like that

    Property Let and get.

    It's just as cool though.

  17. #17
    New Member
    Join Date
    Jul 2002
    Posts
    8
    actually, an array of arrays doesnt have to be a flat data structure. Even a single dimensional array can be setup to represent a binary tree with the right processing algolrithim.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width