Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Location
    Belgium
    Posts
    243

    Unanswered: ByVal versus ByRef

    Hi all,

    I wrote following code to test the difference between byVal and ByRef :

    Private Sub Main()
    Dim var As Byte

    var = 5
    Debug.Print var

    ModifyByVal (var)
    Debug.Print "modified by val : " & var

    ModifyByRef (getal)
    Debug.Print "modified by ref : " & var
    End Sub


    Private Sub ModifyByVal(ByVal var As Byte)
    var = 10
    End Sub

    Private Sub ModifyByRef(ByRef var As Byte)
    var = 10
    End Sub



    I expected the value of var to be 10 after calling the sub ModifiedByRef.
    But it still shows 5.


    I wanted to test this because I have a similar issue in my real program. I passed a recordset by val. In the called sub I do a loop through this recordset till EOF. In my mainsub, also the recordset I passed is EOF????

    Thanks for your help

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I've learned something new here. Hopefully someone can explain, or you can use it to google and find the answer.
    Both of these work, but your syntax does not.
    Code:
    ModifyByRef val
    Code:
    Call ModifyByRef (val)
    However, this is unrelated to your real problem. Bytes (and similar variables) are simple, value types. Recordsets are reference types.

    When you pass a recordset you don't pass the object, you pass a pointer to the object in memory. Whether you pass that pointer by value or reference makes no difference - both still point to the same address location.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I've saved your googling!

    This covers everything, including coercing the ByRef argument of a routine to be ByVal (the bit I've never seen before).
    Passing Variable ByRef And ByBal
    Testimonial:
    pootle flump
    ur codings are working excelent.

Posting Permissions

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