Results 1 to 6 of 6

Thread: Dynamic Arrays in VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    8
    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

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    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]
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  3. #3
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Note:
    Originally posted by PWNettle
    You can only redim the last dimension of a multidimensional array.

    Paul
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    8
    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

  5. #5
    New Member
    Join Date
    Nov 2000
    Posts
    2
    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

  6. #6
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    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
  •  



Click Here to Expand Forum to Full Width