+ Reply to Thread
Results 1 to 16 of 16

Learning differences between ByVal and ByRef

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Learning differences between ByVal and ByRef

    I am trying to learn the differences between ByVal and ByRef. Below is a summary of what I have currently learned (or think I have ):
    1. Change original variable passed as argument: ByVal = can not change (passes a copy of the value only), ByRef = can change (passes the original reference) (hence their names)
    2. Passing Arrays: ByVal = Can't do (error), ByRef = Always use for passing arrays as arguments
    3. Passing objects: ByVal = Can not use on objects? ByRef = Always?
    4. Speed: ByVal = slightly slower, ByRef = slightly faster
    5. Argument type: ByRef = must be same variable type passed? ByVal = ?
    6. If not declared: ByVal = not default, ByRef = Default


    My questions:
    1. If there is anything I have incorrect in the above, could someone please let me know?
    2. Is there anything important missing from my summary that I should be learning?
    3. I fail to see any benefits of using ByVal over ByRef (unless you are paranoid about the possibility of altering the original variable). Or am I missing something big here? Please enlighten me if so.

    Thanks for any help/corrections.
    Last edited by mc84excel; 06-28-2013 at 03:26 AM. Reason: Corrections made
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Learning differences between ByVal and ByRef

    Hello mc84excel,

    ByRef and Byval have to do with "pointers". A pointer is an address to a memory location. Variables will place a pointer on the stack when called.

    ByRef (BY Reference) indicates that the variable has been dimensioned in the procedure and assigned a value or to and object. The main purpose of ByRef is to modify and existing variable or object directly from a remote call.

    ByVal (By Value) indicates that variable has not been created and exists as a constant value within the calling expression. A "copy" is made and stored in a temporary memory location that the calling procedure can access.
    Last edited by Leith Ross; 06-24-2013 at 08:21 PM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Learning differences between ByVal and ByRef

    Passing objects: ByVal = Can not use on objects? ByRef = Always?
    Objects can be passed by value or reference.

    If passed by reference, the procedure receives the actual pointer to the object. You can both change the properties of the object and change the object to which the variable refers. For a range, for example, you can not only set the value, you can set it to different range (e.g., from A1 to A2).

    If passed by value, the procedure receives a copy of the pointer to the object. You can still change the properties of the object (e.g., set the value to "Bob"), but not the object itself.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Learning differences between ByVal and ByRef

    Quote Originally Posted by shg View Post
    Objects can be passed by value or reference.
    I'm puzzled. Chip Pearson states "Objects are always passed by reference. The ByRef and ByVal modifers indicate how the reference is passed to the called procedure. When you pass an object type variable to a procedure, the reference or address to the object is passed -- you never really pass the object itself." (emphasis added) (Source: http://www.cpearson.com/excel/byrefbyval.aspx )

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Learning differences between ByVal and ByRef

    Please Login or Register  to view this content.
    In context, it means that you always get a pointer to the object, but not necessarily the pointer.

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Learning differences between ByVal and ByRef

    Quote Originally Posted by shg View Post
    Please Login or Register  to view this content.
    In context, it means that you always get a pointer to the object, but not necessarily the pointer.
    But isn't the above the same effect as passing by ref? (Perhaps I'd better not worry trying to work out the difference between ByVal & ByRef on objects... )

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Unhappy Re: Learning differences between ByVal and ByRef

    Can anyone answer Question 3? (See post#1).

    If there is no major advantage to ever using ByVal over ByRef, then I may as well permanently adopt ByRef and close the thread.

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Learning differences between ByVal and ByRef

    byRef is the default for VB6 and therefore VBA, whereas byVal is now default for .Net since it follows C++ principles. ByVal is considered safer as it prevents inadvertent changes - but it's horses for courses. Speed difference is really not worth considering here, it's negligible anyway; passing references should be dictated by the code structure rather than anything else.

    But isn't the above the same effect as passing by ref? (Perhaps I'd better not worry trying to work out the difference between ByVal & ByRef on objects... )
    No, it isn't; consider:
    Please Login or Register  to view this content.

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Learning differences between ByVal and ByRef

    Good example Thanks +1

    Just so I've got this correct - if I am not altering or deleting the original argument then it shouldn't really matter if I am using ByVal or ByRef? (I mean apart from very minor speed improvement + coding is sloppier)

  10. #10
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Learning differences between ByVal and ByRef

    I'm not really sure what I'm supposed to be answering here - surely you've answered your own question?

  11. #11
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Learning differences between ByVal and ByRef

    I wasn't sure if I had answered it or not.

    (I'm currently leaning towards using byref by default except where I want to make changes to the passed argument without affecting the value in the caller sub)

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Learning differences between ByVal and ByRef

    byref is the default anyway
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  13. #13
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Learning differences between ByVal and ByRef

    Yes it is.

    What i meant in my previous post - I'd like to fully declare the arguments on my functions and - correct me if I'm wrong - it would do no harm to just enter these as byref? (Except of course where I want to change the parameter value but keep the original argument the same - in which case, I'd use byval)

  14. #14
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Learning differences between ByVal and ByRef

    no it won't do any harm
    Last edited by JosephP; 07-24-2013 at 08:19 PM. Reason: remove ot content

  15. #15
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Learning differences between ByVal and ByRef

    Wince. I guess I was asking for that.

  16. #16
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Learning differences between ByVal and ByRef

    deleted at op request
    Last edited by JosephP; 07-24-2013 at 08:18 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1