"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)
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.
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
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.
Re: "byref argument type mismatch"
Re: "byref argument type mismatch"
Quote:
Originally Posted by
Bruin 1953
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)
Quote:
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.
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.
Re: "byref argument type mismatch"
Quote:
Originally Posted by
aio
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.