PRB:Parameter Mismatch Error When Pass Properties by Reference
ID: Q79597
|
The information in this article applies to:
-
Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
-
Microsoft Visual Basic programming system for Windows, version 1.0
SYMPTOMS
When trying to pass a control property value by reference to a Sub or
Function procedure, you receive a "Parameter Mismatch" 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 "Parameter Mismatch" 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 "Parameter
Mismatch" 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 = "passed by value"
CALL Mysub ((Text1.text))
' Notice Text1.text did not change.
End Sub
Sub Command2_Click()
Text1.text = "passed temporary variable"
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$ = "Changed"
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 :
Version :
Platform :
Issue type :