PDA

View Full Version : Question on redim... in multi dim array

jaywhy13
08-14-2005, 11:29 PM
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?

BaldEagle
08-14-2005, 11:48 PM
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

jaywhy13
08-15-2005, 12:18 AM
So is there a proper way to do what I wanted to do?

BaldEagle
08-15-2005, 01:04 AM
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

jaywhy13
08-15-2005, 03:37 PM
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!