Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2004
    Location
    Jamaica
    Posts
    592
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question on redim... in multi dim array

    Look at this...
    Dim student()
    Redim Preserve student(1,0)
    student(0,0) = "male"
    student(1,0) = "charles"

    Can I now do this...

    Redim Preserve student(2,1)
    student(0,1) = "female"
    student(1,1) = "Jenny"
    student(2,1) = "James"

    Adding a third column for the 2nd row? Can I do that? And if i can do that? Will it also add a third column to the 1st row?
    I'm gonna find a way to download the internet if its the last thing I do...
    Prepare to bow down to me (or my grave) and call me almighty when the algorithm is finished

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jaywhy13
    Look at this...
    Dim student()
    Redim Preserve student(1,0)
    student(0,0) = "male"
    student(1,0) = "charles"

    Can I now do this...

    Redim Preserve student(2,1)
    student(0,1) = "female"
    student(1,1) = "Jenny"
    student(2,1) = "James"

    Adding a third column for the 2nd row? Can I do that? And if i can do that? Will it also add a third column to the 1st row?
    No, you can't do this. When you use the Preserve keyword you can only change the size of the last dimension and still "PRESERVE" the data.

    ReDim Statement Language Reference

    --------------------------------------------------

    Description
    Declares dynamic-array variables, and allocates or reallocates storage
    space at procedure level.
    Syntax
    ReDim [Preserve] varname(subscripts) [, varname(subscripts)] . . .
    The ReDim statement syntax has these parts:

    Part Description
    Preserve Preserves the data in an existing array when you change the
    size of the last dimension.

    varname Name of the variable; follows standard variable naming conventions.
    subscripts Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax:
    upper [,upper] . . .

    The lower bound of an array is always zero.



    Remarks
    The ReDim statement is used to size or resize a dynamic array that has already been formally declared using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts). You can use the ReDim statement repeatedly to change the number of elements and dimensions in an array.
    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. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array.

    The following example shows how you can increase the size of the last dimension of a dynamic array without erasing any existing data contained in the array.

    ReDim X(10, 10, 10)
    . . .
    ReDim Preserve X(10, 10, 15)


    ----------------------------------------------------------------

    Caution If you make an array smaller than it was originally, data in the eliminated elements is lost.

    ----------------------------------------------------------------


    When variables are initialized, a numeric variable is initialized to 0 and a string variable is initialized to a zero-length string (""). A variable that refers to an object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing.

    BaldEagle

  • #3
    Regular Coder
    Join Date
    Dec 2004
    Location
    Jamaica
    Posts
    592
    Thanks
    2
    Thanked 0 Times in 0 Posts
    So is there a proper way to do what I wanted to do?
    I'm gonna find a way to download the internet if its the last thing I do...
    Prepare to bow down to me (or my grave) and call me almighty when the algorithm is finished

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would not consider myself a vbscript expert by any stretch of the imagination, I just happened to know the answer to your original question. I do not know how complex you want your array structure to be, but if it is possible try to make several one dimensional arrays that can be used in place of the multi-dimensional one. This way you can ReDim Preserve without worrying about loosing any data. A this point in my level of knowledge this is how I would approach the problem. There may very well be a better solution that I am unaware of. I have only recently started using vbscript and javascript and I'm still learning, so I'd go with what I know until I know more.

    BaldEagle

  • #5
    Regular Coder
    Join Date
    Dec 2004
    Location
    Jamaica
    Posts
    592
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BaldEagle
    I would not consider myself a vbscript expert by any stretch of the imagination, I just happened to know the answer to your original question. I do not know how complex you want your array structure to be, but if it is possible try to make several one dimensional arrays that can be used in place of the multi-dimensional one. This way you can ReDim Preserve without worrying about loosing any data. A this point in my level of knowledge this is how I would approach the problem. There may very well be a better solution that I am unaware of. I have only recently started using vbscript and javascript and I'm still learning, so I'd go with what I know until I know more.

    BaldEagle
    Your assistance has not gone appreciated.... thanks man!
    I'm gonna find a way to download the internet if its the last thing I do...
    Prepare to bow down to me (or my grave) and call me almighty when the algorithm is finished


  •  

    Posting Permissions

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