Udostępnij za pośrednictwem


Cannot copy the value of 'ByRef' parameter '<parametername>' back to the matching argument because type '<typename1>' cannot be converted to type '<typename2>'

A procedure is declared with a parameter type which cannot be converted back to the calling argument type.

When you define a class or structure, you can define one or more conversion operators to convert that class or structure type to other types. You can also define reverse conversion operators to convert those other types back to your class or structure type. When you use your class or structure type in a procedure call, Visual Basic can use these conversion operators to convert the type of an argument to the type of its corresponding parameter.

If you pass the argument ByRef, Visual Basic sometimes copies the argument value into a local variable in the procedure instead of passing a reference. In such a case, when the procedure returns, Visual Basic must then copy the local variable value back into the argument in the calling code.

If a ByRef argument value is copied into the procedure and the argument and parameter are of the same type, no conversion is necessary. But if the types are different, Visual Basic must convert in both directions. If one of the types is your class or structure type, Visual Basic must convert it both to and from the other type. This means you must define conversion operators in both directions.

Error ID: BC33037

To correct this error

  • If possible, use a calling argument of the same type as the procedure parameter, so Visual Basic does not need to do any conversion.

  • If you need to call the procedure with an argument type different from the parameter type but do not need to return a value into the calling argument, define the parameter to be ByVal instead of ByRef.

  • If you need to return a value into the calling argument, define the reverse conversion operator so Visual Basic can convert back to the calling argument type.

See Also

Tasks

How to: Define an Operator

How to: Define a Conversion Operator

Concepts

Procedures in Visual Basic

Procedure Parameters and Arguments

Passing Arguments by Value and by Reference

Operator Procedures

Reference

Operator Statement