"Parameter Mismatch" Passing Control Properties by Reference

ID Number: Q79597

1.00

WINDOWS

Summary:

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 like their prescribed types when used

directly, they cannot be passed by reference to a SUB or FUNCTION. Any

attempt to do so will generate a "PARAMETER MISMATCH" error.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

Passing by reference, the default parameter passing method in Visual

Basic, places the address of the variable on the stack. The SUB or

FUNCTION 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 will place its handle on the stack instead of an address.

Because the handle uses a different form than an address, the SUB or

FUNCTION accesses a value that it is not expecting, and will generate

a "PARAMETER MISMATCH" error.

As a workaround, 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

to treat it as such. Because an actual memory location is not

transferred to the SUB or FUNCTION, any changes to the value of the

property are localized to that SUB or FUNCTION.

Another workaround is to assign the property value to a temporary

variable. The temporary variable has an actual address and can be

passed to a SUB or FUNCTION 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, the value of the temporary variable must be assigned to the

property variable upon return from the SUB or FUNCTION.

Example

-------

Create a project with one form (Form1), two control buttons (Control1

and Control2), and one text box (Text1). Add the two control Click

events as follows:

Sub Control1_Click ()

Text1.text = "passed by value"

CALL Mysub ((Text1.text))

'notice Text1.text did not change

End Sub

Sub Control2_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

Additional reference words: 1.00