-
January 21st, 2009, 10:49 AM
#1
"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)
-
January 21st, 2009, 11:32 AM
#2
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
-
-
January 21st, 2009, 11:54 AM
#3
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
-
January 21st, 2009, 12:40 PM
#4
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
-
-
January 21st, 2009, 12:58 PM
#5
Re: "byref argument type mismatch"
-
January 22nd, 2009, 02:55 AM
#6
Re: "byref argument type mismatch"
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)
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.
-
January 22nd, 2009, 04:21 AM
#7
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
-
January 22nd, 2009, 09:50 AM
#8
Re: "byref argument type mismatch"
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|