Microsoft KB Archive/79597

= PRB: Parameter Mismatch Error When Pass Properties by Reference =

Article ID: 79597

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q79597



SYMPTOMS
When trying to pass a control property value by reference to a Sub or Function procedure, you receive a &quot;Parameter Mismatch&quot; error.



CAUSE
Control property values in Visual Basic are stored in a formatted form whose location is periodically changed as part of Windows memory management. The values are accessed by handles, not addresses. Although the values behave as their prescribed types when used directly, they cannot be passed by reference to a Sub or Function procedure. Any attempt to do so will generate a &quot;Parameter Mismatch&quot; error.

Passing by reference, the default parameter passing method in Visual Basic, places the address of the variable on the stack. The Sub or Function procedure then accesses the address on the stack and uses it to refer to that variable.

Sending a control property as a parameter to a Sub or Function procedure will place its handle on the stack instead of an address. Because the handle uses a different form from an address, the Sub or Function procedure finds a value that it is not expecting, and will generate a &quot;Parameter Mismatch&quot; error.



RESOLUTION
Pass the property by value instead of by reference. To pass by value, place a set of parentheses around the property variable in the Sub or Function call. This syntax will place the actual value of the property on the stack and tell the Sub or Function procedure to treat it as such. Because an actual memory location is not transferred to the Sub or Function procedure, any changes to the value of the property are localized to that Sub or Function procedure.

As an alternative resolution, assign the property value to a temporary variable. The temporary variable has an actual address and can be passed to a Sub or Function procedure in the usual manner. Because an actual address is sent, any change to the temporary variable will be permanent. In order for the actual property variable to reflect this change, you must assign the value of the temporary variable to the property variable upon return from the Sub or Function procedure.

Step-by-Step Example
 Start a new project in Visual Basic. Form1 is created by default. Place two command buttons (Command1 and Command2), and one text box (Text1) on Form1.  Add the two command Click events as follows: Sub Command1_Click Text1.text = &quot;passed by value&quot; CALL Mysub ((Text1.text)) ' Notice Text1.text did not change. End Sub

Sub Command2_Click Text1.text = &quot;passed temporary variable&quot; temp$ = Text1.text CALL Mysub (temp$) Text1.text = temp$ ' Notice Text1.text did change when assigned to temp$. End Sub   In the General section of Form1, add the following: Sub Mysub(A$) A$ = &quot;Changed&quot; End Sub  Run the program by pressing the F5 key. Click the command buttons to observe the behavior.

Additional query words: 2.00 3.00

Keywords: kbprb KB79597

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.