Pass Array to Sub Routine

I'm trying to pass an array to a Sub from the onclick event of a button --  I'm getting a type mismatch error.

Here is the code for the buttion

<input type=button language=vbscript value=create onclick="myproc <%=aMyArray%>" id=button1>

Here is the Sub I am passing the array to:

<script language='vbscript'>
Sub myproc(aPassed)
 
Dim strFeedback

For i = lbound(aPassed) to ubound(aPassed)
     strFeedback = strFeedback & aPassed(i)
Next

Msgbox strFeedback

End Sub
</script>

aMyArray is a valid array -- I write it to screen before calling the function to verify that it is formatted correctly

I've tried several different ways of calling the sub, but to no avail.  I've tested calling the sub with different params and it works fine.  But when I try to pass an array it bonks with the mismatch error.

Any help would be appreciated

Thanks Brian
bharvforAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rockmansatticCommented:
total left field thought, but have you tried onclick="myproc (<%=aMyArray%>)"

I will look into this to see if I can help.

Rockman
Anthony PerkinsCommented:
When you get it to work, you can simplify this:
Sub myproc(aPassed)
 
Dim strFeedback

For i = lbound(aPassed) to ubound(aPassed)
     strFeedback = strFeedback & aPassed(i)
Next

Msgbox strFeedback

End Sub

To this:
Sub myproc(aPassed)
 
Msgbox Join(aPassed, "")

End Sub

rhencullenCommented:
Another thing you shoud think about, is to make absolutely sure you verify taht your array is a valid array in the Sub before trying to access it's elements.

In the Commercial site I'm responsible for, all Subs and Functions that receive an array have something similar inside at the start of the Sub:

Function doSomethingWithArray( p_Array)
   Dim result = false

   if (IsArray(p_Array) = false) then
      doSomethingWithArray = result
      Exit Function
   end if

   ' Now try to actually do the something, and set the return result to true to indicate success

   ' Do something.
      result = true

   doSomethingWithArray = result

End Function

(I also use Functions most of the time instead of Subs, so the success or failure of the function is returned to the calling routine).

Paul
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

alorentzCommented:
Everyone seems to be missing the fact that you can't pass an Server side array into Client side vbscript like that.  Arrays don't pass between server and client, unless passed as string format like (1,2,3,4,5).

In short, this can't be done as requested.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HypnochuCommented:
Assuming that you are passing in comma-separated values, I would try this:

Sub myproc(aPassed)
        Dim strFeedback
        Dim i
      aPassed = Split(aPassed, ",")
      For i = lbound(aPassed) to ubound(aPassed)
           strFeedback = strFeedback & aPassed(i)
      Next

      Msgbox strFeedback

End Sub
amit_gCommented:
Add this somewhere on the page...

<%
Function ArrayToString(MyArray)
      Dim strArrayToString, I

      strArrayToString = ""

      For I = LBound(MyArray) To UBound(MyArray)
            strArrayToString = strArrayToString & "," & MyArray(I)
      Next

      ArrayToString = Right(strArrayToString, Len(strArrayToString) - 1)
End Function
%>

and change your statement to

<input type=button language=vbscript value=create onclick="myproc Array(<%=ArrayToString(aMyArray)%>)" id=button1>
bharvforAuthor Commented:
Thanks alorentz and others that indicated an array can't be passed to the client.

I ended up just passing a delimited string as suggested.  Out of curiosity what is the reason that other data types can be passed without a problem but not an array.  I haven't come across this in any documentation and was just trying to understand the reasoning.

Thanks again

Brian

alorentzCommented:
Server side code becomes TEXT when passed to client, not an object (which an array is).   Basically, server side code just builds a TEXT version of a page to render for the client to use.  Any code or scripting then needs to happen on the client, because server side and client scripting they do not communicate (in most cases).  An array would need to be rebuilt on the client to be used in client side scripting.

For instance, when you do:
<%
mystring = "Hello"
%>
<%=mystring%>

It just writes the word "Hello" in text to the rendered page....it doesn't pass the variable mystring for use.  in the same sense, object cannot be passed between server and client...unless reintialized in script on client.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.