|
-
Nov 27th, 2000, 12:39 PM
#1
Thread Starter
New Member
I would like to know if it is possible to dynamically create an array in VB with out knowing the number of dimensions ahead of time.
I can Dim intAry() as integer
and then
redim intAry(2,3)
but what if I won't know until runtime how many dimensions there will be?
thanks,
Jeremy
-
Nov 27th, 2000, 12:53 PM
#2
Fanatic Member
Do you mean something like this?
Code:
Dim myArray() As Integer 'the array
Dim X As Integer 'the first dimension
X = CInt(InputBox("Enter a number, plz.")) 'ask what x should be
ReDim myArray(X, 5) As Integer 'redimension the array
Btw, because it's your first post, maybe you don't know how
to format code in a post. You can do it like this:
[code]
'code here
'...
[/code]
-
Nov 27th, 2000, 01:01 PM
#3
Fanatic Member
Note:
Originally posted by PWNettle
You can only redim the last dimension of a multidimensional array.
Paul
-
Nov 27th, 2000, 01:03 PM
#4
Thread Starter
New Member
Thank you for the code markup. Yes it was my first post.
The problem with that code sample is that I have to know how many X's I will have, Not the value of X, but the number of actual dimensions.
I want to do it without knowing that.
Thanks
Jeremy
-
Nov 29th, 2000, 04:29 AM
#5
New Member
You could try with something like this:
dim myarr()
sub ....
...
i=0
while list(i)<>""
i=i+1
wend
i=i-1
redim myarr(i)
...
end sub
Could this be helpfull ?
Filippo
-
Nov 29th, 2000, 06:20 AM
#6
Fanatic Member
There appears to be a bit of confusion here. As long as the array is declared with no dimensions e.g. Dim ary() As SomeType, you can use ReDim (with variables, if required) to add dimensions.
e.g.
Code:
Dim X As Integer, Y As Integer, Z As Integer, ary() As Integer
X = 5
Y = 2
Z = 3
ReDim ary(X, Y, Z)
Using ReDim will re-initialise all values in the array. However, if you use ReDim with Preserve, you can keep the data but you are limited to changing the size of the last dimension only. This means that you cannot add dimensions and use Preserve at the same time. You would have to create a copy of the array, modify the dimensions and then copy the data back in.
BTW oetje, how do you put [ code] in to the body of the message as text? and how do you add a reference to a thread? i.e. how do you know the thread number and what are the tags?
Cheers,
P.
Not nearly so tired now...
Haven't been around much so be gentle...
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
|