HOWTO: Returning Arrays from Server-Side Objects in ASP

Last reviewed: October 3, 1997
Article 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>

Keywords          : AXSFVBS
Technology        : kbDSupport
Version           : WINDOWS:2.0; WINNT:1.0,1.0b
Platform          : WINDOWS winnt
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 3, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.