Microsoft KB Archive/174576

From BetaArchive Wiki
Knowledge Base

HOWTO: Return Arrays from Server-Side Objects in ASP

Article ID: 174576

Article Last Modified on 7/13/2004


  • Microsoft Visual Basic, Scripting Edition 2.0
  • Microsoft Active Server Pages 4.0

This article was previously published under Q174576


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


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:

     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>"

Keywords: kbhowto kbscript KB174576