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



The information in this article applies to:

  • Microsoft Active Server Pages
  • Microsoft Visual Basic Learning Edition for Windows 5.0
  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

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.

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

197957 PRB: Passing Parameters by Reference to a VC COM Object

197956 PRB: Passing Parameters by Reference to a VB COM Object

174576 HOWTO: Returning Arrays from Server-Side Objects in ASP

218454 HOWTO: Implement Array Arguments in VC COM Objects for Active Server Pages

217114 HOWTO: Implement Array Arguments in Visual Basic COM Objects for Active Server Pages


Modification Type:MajorLast Reviewed:11/26/2003
Keywords:kbCodeSnippet kberrmsg kbinfo KB244012