HOWTO: Returning Arrays from Server-Side Objects in ASP

ID: Q174576


The information in this article applies to:
  • Microsoft Visual Basic, Scripting Edition, version 2.0
  • Microsoft Active Server Pages, versions 1.0, 1.0b


SUMMARY

Active Server Pages developers programming in VBScript (VBS) often encounter "Type Mismatch" errors when attempting to return arrays form server-side objects to server-side script code. This article explains how to do this in VBS version 2.0.


MORE INFORMATION

VBS is not as flexible as Visual Basic when it comes to passing variables of different types to or from objects. This is because VBS stores all variables as VARIANTS. Thus, it must convert these VARIANTS to other types when calling methods on objects.

A common scenario is to call a method on a server-side object that returns an array of strings, or some other type, to ASP. Depending on how the server-side object is coded, this may cause a "Type Mismatch" error in the script. The key to making this work, is declaring the object method to return a VARIANT. Inside the method, store the array of data in a VARIANT and return this variable to the server-side script.

The code segments below show a method on a Visual Basic 5.0 object, and the ASP script that calls this method.

Visual Basic 5.0 Object Method:


   Public Function GetArray() As Variant
    Dim MyVar(5) As Variant
    MyVar(0) = "String 0"
    MyVar(1) = "String 1"
    MyVar(2) = "String 2"
    MyVar(3) = "String 3"
    MyVar(4) = "String 4"
    GetArray = MyVar
   End Function 
Active Server Pages Script:

   <HTML>
   <BODY>
   <%
     Set Array_Obj = Server.CreateObject("Project1.Class1")
     myarray = Array_Obj.GetArray()
     Response.Write myarray(0) & "<P>"
     Response.Write myarray(1) & "<P>"
     Response.Write myarray(2) & "<P>"
     Response.Write myarray(3) & "<P>"
     Response.Write myarray(4) & "<P>"
   %>
   </BODY>
   </HTML> 

Additional query words:

Keywords :
Version : WINDOWS:2.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: August 30, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.