Results 1 to 6 of 6

Thread: ByRef argument type mismatch when passing variable [VB6]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Question ByRef argument type mismatch when passing variable [VB6]

    Given the following code: If MatchF("Location", rArray(6)) Then Pass = False
    Where: Public Function MatchF(strField As String, strValue As String)
    This generates the following error “Compile Error: ByRef argument type mismatch”

    Any clues as to why?
    I tried the following code: If MatchF("Location", rArray(6).Value) Then Pass = False
    This generates the following error: “Run-Time Error 424: Object Required”

    All I want to do is pass that part of the Array into the MatchF function to determine if it is a match. The contents of Public Function MatchF should not make any difference nor should the contents @ rArray(6) itself.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    What type of array is rArray ?
    You may need to use string conversion function:

    If MatchF("Location", CStr(rArray(6))) Then

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308
    All I used was : Dim rArray(15)
    (should I declare it AS something? if so what?)

    I thought it would have something to do with my MatchF function.
    Wouldn't I change that to something like:

    MatchF(strField As String, byref strValue As String)
    or something?

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    If type is not specified - VARIANT is assumed.
    You may need to define your array as string:

    Dim rArray(15) As String

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by Shaitan00
    All I used was : Dim rArray(15)
    (should I declare it AS something? if so what?)

    I thought it would have something to do with my MatchF function.
    Wouldn't I change that to something like:

    MatchF(strField As String, byref strValue As String)
    or something?
    Your MatchF function IS FINE, leave it alone. It's the array that's not right. By not setting the data type, you've declared it as a variant. 1) either dim it as an array of string or 2) CSTR the element when passing it to the MatchF function. Either way it's your choice, but the function is fine.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Too bad ByRef won't work to the variant. Of course you can do a CStr, but if the value will be changed, it does not change in the array. A simple example code:

    VB Code:
    1. Private Sub DoAChange(ByRef Something As String)
    2.     Something = "Woot!"
    3. End Sub
    4. Private Sub Form_Load()
    5.     Dim AnArray(0)
    6.    
    7.     DoAChange CStr(AnArray(0))
    8.     Caption = AnArray(0)
    9. End Sub

    Caption will be just empty, because nothing got assigned to the array. All we changed was what CStr() holded, and it wasn't stored anywhere.


    The only solution to the problem is to declare the array as a string array or to change ByRef datatype to Variant.

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