|
-
Jun 5th, 2002, 12:35 AM
#1
Thread Starter
PowerPoster
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:
Private arr() As Variant
Sub AddArrayDimension(ArrayIndex As Long)
Redim Preserve Array(ArrayIndex)(Ubound(Array(ArrayIndex)) + 1)
End Sub
Anyone have any good ideas about how to do this specifically?
-
Jun 5th, 2002, 10:23 PM
#2
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:
ReDim arr(9)
ReDim arr(9).arr(12)
Which is sort of like a multi dimensional array anyway.
-
Jun 5th, 2002, 10:27 PM
#3
PowerPoster
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]
-----------------------------------------
-
Jun 5th, 2002, 10:29 PM
#4
Thread Starter
PowerPoster
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.
-
Jun 5th, 2002, 10:30 PM
#5
PowerPoster
Ah yes, I see. So something like:
arr(2)(3)
arr(2)(1)
arr(2)(5)
arr(2)(3)
?
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Jun 5th, 2002, 10:30 PM
#6
Thread Starter
PowerPoster
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.
-
Jun 5th, 2002, 10:31 PM
#7
Thread Starter
PowerPoster
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.
-
Jun 5th, 2002, 10:39 PM
#8
If the second value is a variable, I think you have to redim the whole variable.
VB Code:
Dim F()
ReDim F(10)
Dim G(10)
F(3) = 3
G(5) = F
MsgBox G(5)(3)
ReDim Preserve F(20)
F(18) = 18
G(5) = F
MsgBox G(5)(18)
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.
-
Jun 5th, 2002, 10:44 PM
#9
Thread Starter
PowerPoster
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:
Private Sub Command1_Click()
Dim intX As Integer ' Declare counter variable.
' Declare and populate an integer array.
Dim countersA(5) As Integer
For intX = 0 To 4
countersA(intX) = 5
Next intX
' Declare and populate a string array.
Dim countersB(5) As String
For intX = 0 To 4
countersB(intX) = "hello"
Next intX
Dim arrX(2) As Variant ' Declare a new two-member
' array.
arrX(1) = countersA() ' Populate the array with
' other arrays.
arrX(2) = countersB()
MsgBox arrX(1)(2) ' Display a member of each
' array.
MsgBox arrX(2)(3)
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.
-
Jun 5th, 2002, 10:58 PM
#10
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.
-
Jun 5th, 2002, 11:13 PM
#11
Thread Starter
PowerPoster
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:
' form
Sub AddArrayValue(ArrayIndex As Long, Value As Variant)
' validate here
TBL.AddArrayValue ArrayIndex, Value
End Sub
' cTable
Public Sub AddArrayValue(ArrayIndex As Long, Value as Variant, Optional Position As Long)
With m_Array(ArrayIndex)
' need to add a subscript here
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.
-
Jun 5th, 2002, 11:20 PM
#12
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.
-
Jun 5th, 2002, 11:27 PM
#13
Thread Starter
PowerPoster
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.
-
Jul 10th, 2002, 10:01 PM
#14
-
Jul 10th, 2002, 10:03 PM
#15
Thread Starter
PowerPoster
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!
-
Jul 10th, 2002, 10:06 PM
#16
Thread Starter
PowerPoster
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.
-
Jul 10th, 2002, 11:29 PM
#17
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|