"byref argument type mismatch"
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: "byref argument type mismatch"

  1. #1
    Join Date
    Sep 2006
    Posts
    32

    "byref argument type mismatch"

    I always thought that (x&) is passable as an argument to a Sub expecting (x As Long)!
    I was wrong (I think!).
    I got the dreaded "byref argument type mismatch" from this code:


    Private Sub Form_Load()
    Dim txt$
    txt$ = "12345"
    dim i as long
    i = 100
    Call ci(txt$, 1, i)
    End Sub
    Sub ci(txt$, Optional x&, Optional y&)
    ' do something
    End Sub




    The error seems to occur before i assumes any value.
    I solved it by either this:
    Dim i&

    OR this:
    Sub ci(txt$, Optional x AS LONG, Optional y AS LONG)

    In other word, (&) and (AS LONG) do NOT seem to be equal!

    Please explain! (I use VB6)

  2. #2
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,055

    Re: "byref argument type mismatch"

    I think it's becasue y& is optional in Sub ci -- optional parameters are always variant, ergo you get the type mismatch.
    Marketing our skills - please participate in the survey and share your insights
    -

  3. #3
    Join Date
    Sep 2006
    Posts
    32

    Re: "byref argument type mismatch"

    Thanks for trying, but did not work!

    Here is the simplified code (VB6):



    Private Sub Form_Load()
    Dim i, x, y As Long
    x = 5: y = 2
    i = Total(x, y)
    End Sub

    Function Total(x2&, y2&) As Long
    Total = x2& + y2&
    End Function

  4. #4
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,055

    Re: "byref argument type mismatch"

    In youre subsequent code, this...
    Code:
    Dim i, x, y As Long
    i is treated as variant
    x is treated as variant
    y is treated as long

    therefore..
    Code:
    Function Total(x2&, y2&) As Long
        Total = x2& + y2&
    End Function
    ... is incorrect.

    Try this...
    Code:
    Dim i As Long
    Dim x As Long
    Dim y As Long
    ..then call your function Total using your code.
    Marketing our skills - please participate in the survey and share your insights
    -

  5. #5
    Join Date
    Sep 2006
    Posts
    32

    Re: "byref argument type mismatch"

    Bingo!
    Thanks!

  6. #6
    Join Date
    Mar 2006
    Posts
    47

    Re: "byref argument type mismatch"

    Quote Originally Posted by Bruin 1953 View Post
    I always thought that (x&) is passable as an argument to a Sub expecting (x As Long)!
    I was wrong (I think!).
    I got the dreaded "byref argument type mismatch" from this code:

    Private Sub Form_Load()
    Dim txt$
    Dim i, x, y As Long

    txt$ = "12345"
    x = 5: y = 2

    i = ci(txt$, x, y)
    End Sub


    Function ci(txt$, Optional x&, Optional y&)
    MsgBox (Str$(x&) & Str$(y&))
    End Function




    The error seems to occur before i assumes any value.
    I solved it by either this:
    Dim i&

    OR this:
    Sub ci(txt$, Optional x AS LONG, Optional y AS LONG)

    In other word, (&) and (AS LONG) do NOT seem to be equal!

    Please explain! (I use VB6)
    Hi,
    I think the problem with your variable declaratin.

    in vb6, if multiple variables are declared in a single statement then the last varialbel is declared with the specified type and rest will become a variant (an untyped variable)

    Dim i, x, y As Long
    Here only y is declared as long whereas i and x are declared as a variant

    take note of this and try changing your code accordingly.

  7. #7
    Join Date
    Aug 2000
    Location
    Essex, Uk
    Posts
    1,214

    Re: "byref argument type mismatch"

    Also for maintainability it's better to code the long hand way, that way when you come back to your code in a year's time you know exactly what you where passing and dont have to revert to the online help to remind you what the shorthand syntax means.
    If you find my answers helpful, dont forget to rate me

  8. #8
    Join Date
    Dec 2001
    Posts
    6,332

    Re: "byref argument type mismatch"

    Quote Originally Posted by aio View Post
    I think it's becasue y& is optional in Sub ci -- optional parameters are always variant, ergo you get the type mismatch.
    Actually, optional parameters are not always Variant.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center