Forum Moderators: open

Message Too Old, No Replies

Feed Values into Array

         

ukgimp

3:28 pm on May 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



New to ASP (again :)), usually dabble in php. So arrays are confusing me a little.

I have a couple of questions.

Do I have to set the number of values in the array when I dim the thing?:

Dim myArray(10)

or is there a way just to read the values into the array, like in php where you simply run through the values

myArray[] = "string value";

Any good resources covering this sort of thing.

Cheers

john_k

3:54 pm on May 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to specify the number of elements. You can do that with the original Dim, or you can do it with a ReDim command.

If you are getting values from a recordset, you can populate the array with the .GetRows method. That will return a properly dimensioned array.

john_k

3:55 pm on May 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops - there is one other way, and that is with the Split command. So if you are processing a delimitted list, then that works well.

ukgimp

3:57 pm on May 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks John. Just seems lame that I have to know the number of items before I declare it. These wont be CSV array values or from a DB.

Cheers

john_k

4:05 pm on May 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't need to know the number when you declare it with the Dim. Just with the ReDim.


Dim vArray
Dim lArrayLen
Dim lIdx

' detemine number of elements
lArrayLen = DoSomeCalculation()

' Redim and then loop
ReDim vArray(lArrayLen - 1)
For lIdx = 0 To (lArrayLen - 1)
' do stuff to populate array
Next

Another option is the Dictionary object. This allows you to add values or objects without specifying any size.

streetshirts

10:25 pm on May 20, 2004 (gmt 0)

10+ Year Member



You can also define an array like this (usuful if you have a fixed number of items):

Dim myArray
myArray = Array(variable1, "string1", variable2)