Microsoft KB Archive/244012

= INFO: Type Mismatch Errors When You Pass Parameters from ASP to a Visual Basic Component =

Article ID: 244012

Article Last Modified on 11/26/2003

-

APPLIES TO


 * Microsoft Active Server Pages 4.0
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q244012



SUMMARY
The code sample below illustrates various scenarios that causes

Type Mismatch

errors with method calls from an Active Server Page (ASP) using Visual Basic Script to Visual Basic COM components. 'VB Code : [Project=prjParam;Class=clsParam] 'By default, the parameter is passed 'ByRef'

Sub x( a as string ) a = "Changed" End Sub

'ASP Code k = "Hello"

'Create the above VB object

Set obj = Server.CreateObject("prjParam.clsParam")

obj.x k       'Type Mismatch error occurs

obj.x (k)     'Using PARANTHESIS forces 'ByVal', 'k' does not change

Call obj.x (k) 'Type Mismatch error occurs

Call obj.x cstr(k)  'The CSTR function returns a string, 'the address of the variable (k) is not passed. 'The value of 'k' doesn't change

Set obj = Nothing The following is another example that can cause the error: 'VB Code : [Project=prjParam;Class=clsParam] 'If you do not specify, by default the parameter is passed 'ByRef' 'Note: Parameter type is VARIANT

Sub y( a as variant ) a = "Changed" End Sub

'ASP Code k = "Hello"

'Create the above VB object

Set obj = Server.CreateObject("prjParam.clsParam")

obj.y k       'changes 'k'

obj.y (k)     'Using PARANTHESIS forces 'ByVal', 'k' doesn't change

Call obj.y (k) 'changes 'k'

Set obj = Nothing



MORE INFORMATION
VBScript only supports VARIANT ByRef parameters. You can use VBScript to call a procedure that takes ByRef strings, but the default behavior of components built with Visual Basic is to fail with a type mismatch error when trying to pass ByRef parameters to these components. OLE Automation's default type-coercion function fails when asked to convert a ByRef variant into any other ByRef type.

VBScript does not impose this restriction. However, it is the default behavior of the component that decides that a ByRef variant cannot be converted into a ByRef string.

If a parameter to a procedure is enclosed in parenthesis, the parameter is first evaluated as an expression. Because the result of an expression cannot be passed by reference, the parameter is passed ByVal and no error is reported.

Avoid using ByRef parameters unless they are explicitly needed. This is because:
 * ByRef procedures cause more overhead during cross-process marshaling, because COM must marshal the value both to and from the object. ByVal parameters require only one-way marshaling.

However, if you are willing to accept the overhead of any marshaling at all, an extra parameter marshal back is unlikely to cause a huge performance degradation.
 * ByRef parameters can introduce hard-to-find bugs in your code if you accidentally change the value of one of the parameters.
 * JScript does not support ByRef parameters of any type, so if you plan to write components that will support JScript, you should not use ByRef parameters at all.

