Passing arrays through functions

H

Henry

Hi,

I need to pass an array to a function, change it a little and return it to
the main code. When i try it the webpage is displayed up until the function
call. Nothing after that is displayed. When debugging in VS 6 it says: "An
exception of type 'Error in Microsoft VBScript: Incompatible types' was not
handled".

'Main code
'-----------------------------------------------------
dim list(8)
dim newlist(8)

Randomize
For counter = 0 To 4
list(counter) = Int(Rnd * 21)
Response.Write list(counter) & ", "
Next

Response.Write "<br>"
newlist = listFunction(list)

For counter = 0 To 4
Response.Write newlist(counter) & ", "
Next


'function
'-----------------------------------------------------
Function listFunction(list)
dim tmplist(8)

Randomize
For k = 0 To 4
tmplist(k) = list(k)*2
Next

listFunction = tmplist
End Function


Also, when reloading the page i sometimes get the message 'A RPC (Remote
Procedure Call) was not executed' (the message is translated from swedish by
me). Does anyone know what i am doing wrong? Any help is welcome.

/H
 
R

Ray at

Try dimming newlist like so:

'Main code
'-----------------------------------------------------
dim list(8)
dim newlist

Randomize
....

Ray at work
 
H

Henry

Thanks Ray. That worked fine. Would you care to explain why that works and
not my initial code?

/Henrik
 
R

Ray at

I think it's just one of the rules that once you declare a variable as an
array, you cannot go and assign the whole array as something else, even if
it is an array. By dimming it without the (), it is just a variant, and you
can convert that variant into an array by "copying" the existing array.

Ray at work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top