Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to pass an array

Status
Not open for further replies.

Hasfurt

IS-IT--Management
Mar 21, 2005
12
US
Was wondering if I could get some help on this simple code.

I am trying to learn how to pass an array?
Im getting error "subscript out of range"

option explicit
dim myArray (13)
dim i
call var (myarray())
for i = 0 to 12
wscript.echo myarray(i)
next
sub var (arrayVAR())
dim x, j
x = 0
for j = 0 to 12
x = x + 1
arrayvar(j) = x + 5
next
redim arrayvar(50)
end sub

I searched through some therds and couldn't find any help, but if there is already a therd please just give me the link.
Thanks for any help.
 
Hello Hasfurt!

Simply leave off the parentheses in the routine call and you should be good!
Code:
call var (myarray)

Good luck!

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
Minimal correction to Hasfurt's submission.
[tt]
option explicit
dim myArray ()
dim i
call var (myarray)
for i = 0 to 12
wscript.echo myarray(i)
next
sub var (arrayVAR)
dim x, j
x = 0
redim arrayVar(12)
for j = 0 to 12
x = x + 1
arrayvar(j) = x + 5
next
redim preserve arrayvar(50)
end sub
[/tt]
 
I am now trying to redim the array inside the loop with minimal sucess?

It is only passing the last index of the array?

option explicit
dim myArray ()
dim i

call var (myarray)

for i = 0 to 12
wscript.echo myarray(i)
next


sub var (arrayVAR)
dim x, j, ad
x = 0


for j = 0 to 12
x = x + 1
redim arrayVar(j+1)
arrayvar(j) = x + 5
next

redim preserve arrayvar(x)

end sub
 
Hello HasFurt!

Since your array is dynamic now (your Dim statement does not specify the number of elements), you should not need to redim your array.

Good luck!

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
>redim arrayVar(j+1)
redim preserve arrayVar(j+1)
What do you want to do with index 0? vbs array is zero-based indexed.
How about more reading on your own? We do not encourage student-like posting.
 
When I rem the Redim statement just before the last loop I get subscript out of range error again. I can However redim the array a value of 500 but that is not how I would like it. I would like the array to adjust to the size I need it.
 
Put your parentheses back in to this declaration...
Code:
sub var (arrayVAR())

Good luck!

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
I unserstand that my questions might be small but if anything Im not a student to VB I am to Vb script which seems to run differently. I have the microsoft documentation that is not helping me. I have done numerous google searches. In my post in the begging I said if their is another post about this please refer me.

option explicit
dim myArray ()
dim i

call var (myarray)

for i = 0 to 12
wscript.echo i & " " & myarray(i)
next


sub var (arrayVAR())
dim x, j, ad
x = 0
'redim arrayvar (12)
for j = 0 to 12
x = x + 1
redim arrayvar(x+1)
arrayvar(j) = x + 5
msgbox j & " " & arrayvar(j)
next

redim preserve arrayvar(j)

end sub

In the bolded part this when I run the program shows that the values are being added to the array.

The red part is indicating that when I run the program like it is, it is not working. But, I want to be able to redifne the size of the array within the loop.

The blue part shows that if I have that part as part of the program and take out the red then the program works fine.

My real question is can I redefine the array within a loop and have that array be passed back up to the call statement?

Sometimes it is assigning the varibales to the array but only passing up the last part in the index of the array?
 
Replace this:
redim arrayvar(x+1)
By this:
ReDim Preserve arrayVAR(x+1)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top