Help and Support
 

powered byLive Search

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

Article ID:244012
Last Review:November 26, 2003
Revision:3.1
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
				

Back to the top

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.

Back to the top

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
197957 (http://support.microsoft.com/kb/197957/EN-US/) PRB: Passing Parameters by Reference to a VC COM Object
197956 (http://support.microsoft.com/kb/197956/EN-US/) PRB: Passing Parameters by Reference to a VB COM Object
174576 (http://support.microsoft.com/kb/174576/EN-US/) HOWTO: Returning Arrays from Server-Side Objects in ASP
218454 (http://support.microsoft.com/kb/218454/EN-US/) HOWTO: Implement Array Arguments in VC COM Objects for Active Server Pages
217114 (http://support.microsoft.com/kb/217114/EN-US/) HOWTO: Implement Array Arguments in Visual Basic COM Objects for Active Server Pages

Back to the top


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 Enterprise Edition for Windows 6.0

Back to the top

Keywords: 
kbcodesnippet kberrmsg kbinfo KB244012

Back to the top

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by E-mail, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.