CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2000
    Posts
    292

    Using a Two-Dimensional Dynamic Array

    Hi.

    How will I declare a two-dimensional dynamic array and redimension it later? For instance, I have an array like this:

    Array1(x,2)

    where there are two columns fixed, and a variable number of rows which can later be redimensioned, to something like this:

    x=x+1
    Redim Preserve Array1(x,2)

    but I got this error:

    Run-time error '9':
    Subscript out of range

    if I declared my array like this:

    Dim Array1() as String


    If I declared my array like this:

    Dim Array1(0,2) as String

    I got a Compile error saying that the array was already dimensioned when I reached the Redim line.

    Any help is appreciated!



  2. #2
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: Using a Two-Dimensional Dynamic Array

    You are getting an error because of the keyword Preserve. If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. That means you can not change the first dimension, i.e x, but you can change the second dimension, in this case 2.


  3. #3
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: Using a Two-Dimensional Dynamic Array

    I forgot to add to my previous message the suggestion of switching dimensions so your array becomes Array1(2,x) then if x=x+1 your Redim Preserve Array1(2,x) should work.


  4. #4
    Join Date
    Mar 2000
    Posts
    292

    Re: Using a Two-Dimensional Dynamic Array

    Thanks for the reply.

    I have two questions though:
    1) I removed the Preserve keyword. Yes, it worked during the array's content initialization to an empty string (""). This worked fine because there were really no values yet. What if the array has valid values already and I really don't want these values to be lost when I redimension it?

    2) Does switching dimension also mean switching the row and column references? Does my intention of having Array1(row,col) will now mean Array1(col,row)? My column reference is fixed, while it is the row that varies. And how will I declare my array? Is this correct ---> Dim Array1() as String


  5. #5
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: Using a Two-Dimensional Dynamic Array

    You start with:
    Dim Array1() as String 'Correct start
    x=100 'put whatever value you want
    Tweek your code so that you fill the two dimensional array with data with the first dimension as 2 and second as x.
    Redim Preserve Array1(2,x) 'This will work
    x=x+1
    Redim Preserve Array1(2,x) 'will work also
    So you need to switch from Array1(row,col) to Array1(col,row) and adjust your code to reflect that and that should be trivial.



  6. #6
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: Correction to previous posting

    You start with:
    Dim Array1() as String 'Correct start
    y=100 'put whatever value you want
    Redim Array1(2,y) 'here initialize dimensioning your array
    'Tweek your code so that you fill the two dimensional array with data with your rows fixed to 2 and columns set to y.
    x=70 'put whatever value you want
    Redim Preserve Array1(2,x) 'This will work and preserve your data
    x=x+1
    'if you add more data
    Redim Preserve Array1(2,x) 'will work also
    So you need to switch from Array1(row,col) to Array1(col,row) and adjust your code to reflect that and that should be trivial.




  7. #7
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: Using a Two-Dimensional Dynamic Array

    Q1im MyArray () as string
    ' loop to fill the array
    ' *** Redim - Preserve part ***
    'If (no elements added to the array) then
    ReDim MyArray(1, 0) as string
    'else
    ReDim Preserve MyArray(1, UBound(MyArray, 2) + 1) as string
    'End If
    ' code to add the elements to the array



    Q2:
    only the latter dimension is getting redimensioned so u have to use the Row - Column Accordingly
    Srinika



    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  8. #8
    Join Date
    Mar 2000
    Posts
    292

    Re: Correction to previous posting

    Thanks again. I tried your code suggestion and it worked! I just have a follow-up question. What if not only one but both dimensions of a two-dimensional array vary? For instance, everytime a user inputs dimensions x,y, the array will be redimensioned. How will you use the Redim statement with the Preserve keyword? When I removed the Preserve keyword, there was no error. If I put Preserve, I got the "Subscript out of range" error.


  9. #9
    Join Date
    Mar 2000
    Posts
    292

    Re: Using a Two-Dimensional Dynamic Array

    Thank you very much for your code suggestion. I have learned now that only the last dimension can be manipulated.


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

Featured