Results 1 to 10 of 10

Thread: [RESOLVED] Byref argument type mismatch

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Byref argument type mismatch

    When running the code below I get the error shown in the attached image.
    The variable index1 shown in red below gets highlighted when the error occurs.
    Any hint will be appreciated.
    Code:
    Private Function Sub1(index1) As Boolean
        '...
        For i=0 To Ubound(udtvar)
            Sub2 index1, udtvar(i)
        Next
        '...
    End Function
    
    Private Sub Sub2(ind As Integer, ud As myUDT)
    '...
    '...
    End Sub2
    '
    'Elsewhere, in a module:
    Public Type myUDT
        tr As Single
        matrix() As Single
        n1 As Integer
        n2 As Integer
        x() As Single
        y() As Single
    End Type
    Public udtvar() As myUDT
    Attached Images Attached Images  
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,941

    Re: Byref argument type mismatch

    index1 is a variant, when you should be passing an integer

    either type index1 as integer or convert it (cint)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Byref argument type mismatch

    Quote Originally Posted by westconn1 View Post
    index1 is a variant, when you should be passing an integer

    either type index1 as integer or convert it (cint)
    All right, I thought it had something to do with the udt.

    So I have retyped it as Sub1(index1 As Integer) and the error is gone.

    But I'm surprised that index1 is treated as a variant, as it had already been defined at module level as integer. Do you mean that its 'integerness' must be stated every time it is passed as an argument to a function or subroutine?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: Byref argument type mismatch

    You have defined it at module level but in this function you are redefining it as a variant and hence the error...

    If you don't specify it will always take it as a variant.

    Also avoid using module level declarations if you are planning to declare a variable in individual subs/functions...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Byref argument type mismatch

    Although you have defined index1 as Integer at module level you have also defined index1 as a Variant parameter in sub1. They will be considered as different variables with the same name, the one defined in Sub1 will take precedence over the one defined at module level.

    If Sub1 will always work with the module level Index1 then simply remove the parameter from the Sub declare. If Sub1 sometimes works with other variables then keep the parameter but change the name so there is no conflict.

    edit: ooh hello there Koolsid, you beat me to it.
    W o t . S i g

  6. #6

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Byref argument type mismatch

    OK, but in the same application I have this code,
    Code:
    Private Sub Sub3()
        Dim udtvar() As myUDT
        '...
        ReDim udtvar(0)
        '...
        Sub4  udtvar(0)
        '...
    End Sub
    
    Private Sub Sub4(u As myUDT)
    '...
    '...
    End Sub2
    '
    'Elsewhere, in a different module:
    Public Type myUDT
    '...
    '...
    '...
    End Type
    Now I get the same error as before at the blue statement, even though udtvar has been defined locally.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: Byref argument type mismatch

    Not sure... I need to see rest of your code...

    But Try this...

    ReDim udtvar(0) as myUDT
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,941

    Re: Byref argument type mismatch

    you should not use the same variables for global and local

    invent some more names for them
    if it is declared globally no need to pass it to sub or function
    a locally declared variable will override the use of a globally declared variable of the same name

    have you populated th udt?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Byref argument type mismatch

    I'm really sorry that I have slightly mistranscribed the code the second time. Actually it should be:
    Code:
    Private Sub Sub3()
        Dim udtvar() As myUDT
        '...
        For i = 0 To Ubound(udtvar)
            Sub4  udtvar(i)
        Next
        '...
    End Sub
    
    Private Sub Sub4(u As myUDT)
    '...
    '...
    End Sub2
    Simply changing:

    Private Sub Sub4(u As myUDT)

    to

    Private Sub Sub4(u() As myUDT)

    took care of the error.

    At any rate, all this is kind of tricky or maybe I'm kind of messy of both.

    Thank you guys for your time.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  10. #10
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: [RESOLVED] Byref argument type mismatch

    Ah yes...

    u() As myUDT

    I missed that
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

Posting Permissions

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



Click Here to Expand Forum to Full Width