Array and 800A005 Runtime Error

C

Carl Revell

Please note: The example below refers to .NET code, but I think the issue is
VBScript/COM/Array/Variant related, hence my posting in this Newsgroup
rather than a .NET one.

I am trying to get a simple (for now) ASP page working with a .NET class
registered for COM Interop. I've done this before successfully but have run
into a problem with a function in my .NET component which takes an Array as
a parameter. The functionality of the following code is purely to show the
issue - it doesn't do anything "useful" at all.

Here's the very simple .NET class code...

Public Class Class1
Public Function Names(ByVal theArray() As Object) As String
Dim i As String
For Each i In theArray
Names &= i & " "
Next
Return Names
End Function
End Class

This simply takes an array (of strings ultimately) and returns them as one
string, separated by a space.

Here are two examples of simple ASP to call the component that all fail with
an error (see below)

' Example 1 - FAILS
Dim myObj : Set myObj = Server.CreateObject("InteropTest.Class1")
Dim myArray(2)
myArray(0) = "Tom"
myArray(1) = "Dick"
myArray(2) = "Harry"
Response.Write myObj.Names(myArray)

' Example 2 - FAILS
Dim myObj : Set myObj = Server.CreateObject("InteropTest.Class1")
Dim myArray
myArray = Array("Tom","Dick","Harry")
Response.Write myObj.Names(myArray)

Both fail with this error:-

a.. Error Type:
Microsoft VBScript runtime (0x800A0005)
Invalid procedure call or argument: 'Names'
/InteropTest/InteropTest/InteropTest.asp, line 24

However, THIS version of the ASP works fine!?!?!

' Example 3 - WORKS
Dim myObj : Set myObj = Server.CreateObject("InteropTest.Class1")
Response.Write myObj.Names(Array("Tom","Dick","Harry"))

Somehow, if I copy Array("Tom","Dick","Harry") to a variable first, the
function call fails but if I pass Array("Tom","Dick","Harry") directly as
the function parameter it works!?!? Duh!

Q. "Why don't you just use the version that works then...?"

A. I'm trying to pass an array returned by the ADO RecordSet GetRows()
method to the .NET class method and, unfortunately, it behaves like the
first two examples - it fails.

Any help very much appreciated!

Thanks,

Carl
 
B

Bob Barrows

Carl said:
Please note: The example below refers to .NET code, but I think the
issue is VBScript/COM/Array/Variant related, hence my posting in this
Newsgroup rather than a .NET one.

I am trying to get a simple (for now) ASP page working with a .NET
class registered for COM Interop. I've done this before successfully
but have run into a problem with a function in my .NET component
which takes an Array as a parameter. The functionality of the
following code is purely to show the issue - it doesn't do anything
"useful" at all.

Here's the very simple .NET class code...

Public Class Class1
Public Function Names(ByVal theArray() As Object) As String
Dim i As String
For Each i In theArray
Names &= i & " "
Next
Return Names
End Function
End Class

This simply takes an array (of strings ultimately) and returns them
as one string, separated by a space.

Here are two examples of simple ASP to call the component that all
fail with an error (see below)

' Example 1 - FAILS
Dim myObj : Set myObj = Server.CreateObject("InteropTest.Class1")
Dim myArray(2)
myArray(0) = "Tom"
myArray(1) = "Dick"
myArray(2) = "Harry"
Response.Write myObj.Names(myArray)

' Example 2 - FAILS
Dim myObj : Set myObj = Server.CreateObject("InteropTest.Class1")
Dim myArray
myArray = Array("Tom","Dick","Harry")
Response.Write myObj.Names(myArray)

Both fail with this error:-

a.. Error Type:
Microsoft VBScript runtime (0x800A0005)
Invalid procedure call or argument: 'Names'
/InteropTest/InteropTest/InteropTest.asp, line 24

However, THIS version of the ASP works fine!?!?!

' Example 3 - WORKS
Dim myObj : Set myObj = Server.CreateObject("InteropTest.Class1")
Response.Write myObj.Names(Array("Tom","Dick","Harry"))

Somehow, if I copy Array("Tom","Dick","Harry") to a variable first,
the function call fails but if I pass Array("Tom","Dick","Harry")
directly as the function parameter it works!?!? Duh!

Q. "Why don't you just use the version that works then...?"

A. I'm trying to pass an array returned by the ADO RecordSet GetRows()
method to the .NET class method and, unfortunately, it behaves like
the first two examples - it fails.

Have you tried
Response.Write myObj.Names(rs.GetRows)

Have you looked at these articles?
http://support.microsoft.com/default.aspx?scid=kb;en-us;197956
http://support.microsoft.com/default.aspx?scid=kb;en-us;244012
http://support.microsoft.com/default.aspx?scid=kb;en-us;243548

FWIW, that "Names" property/sub name sounds suspiciously like a reserved
keyword (I may be wrong). It probably has nothing to do with your problem
but I just thought I would mention it.
 
C

Carl Revell

Bob,

Thanks for the response which proved very fruitful.

The second article link you published
(http://support.microsoft.com/default.aspx?scid=kb;en-us;244012) explained
how if you wrap parameters in parentheses, they are first evaluated as an
expression which can't then be passed ByRef and so is passed ByVal.

So, this fails:-

Dim myObj : Set myObj = Server.CreateObject( "InteropTest.Class1" )
myArray = Array( "Tom", "Dick", "Harry" )
Response.Write myObj.Names( myArray )

and this (note extra parentheses around myArray) works!!!

Dim myObj : Set myObj = Server.CreateObject( "InteropTest.Class1" )
myArray = Array( "Tom", "Dick", "Harry" )
Response.Write myObj.Names( ( myArray ) )

Unbelievable!

I assume that VBScript tries to pass myArray ByRef, but the variable myArray
is already effectively a reference to the beginning of the array, so in
effect it's passing the reference of a reference and the .NET component
doesn't like it? I've tried declaring the method parameters as ByRef and
ByVal (I've tried both although I don't need to change the original array in
the method so ByVal is fine for me) but I still need to wrap the parameter
in parentheses within the ASP script in order for it to work.

Thanks again Bob, hopefully I can get on and finish the application now!

Carl Revell
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top