How to Avoid the "ByRef Argument Type Mismatch" Error

Last reviewed: October 30, 1995
Article ID: Q138535
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, version 3.0

SUMMARY

If you pass a parameter to a function or sub procedure by reference, the type of the actual parameter passed and the corresponding function argument must match. Otherwise, you will get a "ByRef argument type mismatch" error.

MORE INFORMATION

The reason types have to match with ByRef parameters is that the called procedure is working on the original outside variable through a reference pointer. In the following step-by-step example, if the procedure thinks it is modifying a variant but the outside variable is really a control, the data will probably be ruined.

If something is passed by value, Visual Basic can do automatic type conversion. When you pass by value, the inside procedure is working on a copy and can therefore modify it in any way, such as converting the passed object to a temporary Variant and working on that.

This applies to simple built-in types as well as objects. Problems like this are easier to understand and debug if you set Option Explicit and declare every variable type explicitly.

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add the following code to the General Declarations section of Form1:

       Sub test(x As Control)
           x.Text = "hello"
       End Sub
    
    

  3. Add the following code to the Form_Click event of Form1:

       Private Sub Form_Click()
           For Each object In Controls
               test object
           Next
       End Sub
    
    

  4. Press F5 to run the program. Click Form1. The "ByRef argument type mismatch" error appears because the type of object passed is a Variant by default and the argument to test() is of type Control.

  5. To avoid this error, you can do one of the following:

        - Pass the control ByVal:
    

          Sub test(ByVal x As Control)
              x.Text = "hello"
          End Sub
    
          -or-
    
        - Dimension the object as a Control variable so that the types match:
    
          Private Sub Form_Click()
              Dim object As Control
              For Each object In Controls
                 test object
              Next
          End Sub
    


Additional reference words: 3.00 4.00 vb4win vb4all
KBCategory: kbprg kbhowto kbcode
KBSubcategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 30, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.