+ Reply to Thread
Results 1 to 6 of 6

behind the scenes in passing byref or byVal

  1. #1
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    behind the scenes in passing byref or byVal

    It is commonly explained that passing an object ByRef passes a pointer to an object and passing an object ByVal passes a copy of the object.

    OR IS THAT EXPLANATION REALITY?

    I am wondering if in passing ByVal, VBA actually copies the object before a subroutine is enterred and then VBA passes a pointer to that copied object just as VBA does with passing ByRef.
    The only difference I can see is the location in which an object is copied and overhead in ensuring scope is maintained.

    Someone who really knows what is happening might be able to explain the benefits of where the object copy is actually made and scope controlled.

    Another interesting thought is that it might be nice to be able to pass a reference but forbid changes. To me that might speed up execution compared to passing by value if one desires only to read..... and yet keep code clear (ie: one knows which code is giving or receiving from where). Yes global variables are less demanding on the coder, but trying to identify who is changing the global variable (that was not supposed to be changed) might be tricky if the project is large? I don't know as i am a novice. just trying to write code that might minimize hunting for errors.

    bil

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: behind the scenes in passing byref or byVal

    Quote Originally Posted by whburling View Post
    It is commonly explained that passing an object ByRef passes a pointer to an object and passing an object ByVal passes a copy of the object.
    Reading your post, I don't think you mean "object." I think you mean "scalar variable." In computer science it is important to use precise language. Passing Object and array variables ByRef vs. ByVal is a whole discussion unto itself, which I'll be happy to get into if you are interested.

    By Reference and By Value are passing mechanisms that are not unique to VBA, they are terms used for any language, although language support varies. For example, ALGOL also had By Result and By Value-Result. In C, the default is by value, and a symbol (not keyword) is needed to indicate a pass by reference.

    The default mode in VBA is By Reference, so a pointer is passed. Any changes made in the called procedure to that variable will affect the caller. You may also explicitly specify ByRef.

    Using ByVal creates a copy of the variable in the stack frame of the called procedure so that any changes to it are local to that procedure.

    I am wondering if in passing ByVal, VBA actually copies the object before a subroutine is enterred and then VBA passes a pointer to that copied object
    You are really getting down into the weeds here. At some level everything is a pointer. So don't think of it like this.

    Another interesting thought is that it might be nice to be able to pass a reference but forbid changes.
    Interesting indeed but I do not know of a language that does this. There really isn't any advantage to it because:
    To me that might speed up execution compared to passing by value if one desires only to read
    Probably not. Creating a copy of a pointer to a variable in the stack frame is not really any faster than creating a copy of the variable itself.

    trying to identify who is changing the global variable (that was not supposed to be changed) might be tricky if the project is large?
    People often use global variables because it's easier to code, but it's usually not a good practice. (Note that I did not say "always.") This is what was identified by Ed Yourdoun back in 1979 as "common environment coupling" and causes exactly the problem you have described. Bugs can be very difficult to diagnose because when a global variable is determined to have an unexpected value in one place, it can be extremely tedious to determine where and when this value was assigned. Two different code modules may make conflicting assumptions about how this variable is to be handled. There are loads of opportunities for things to go wrong.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: behind the scenes in passing byref or byVal

    I guess I do not learn very quickly. I lost the last three attempts to respond to you, 6Strings.
    By the way, in one of those replies, I wondered if you have ever attempted to interact (play your guitar) with
    A bird that might be singing by an open window. I wonder if the bird would actually hear you and respond to
    Your reply to his or hers. It seems us humans only hear what we are seeking to hear. Maybe birds can not
    Hear a response as it is so determined to listen to a familiar static response. I would love to imagine birds
    Are more adaptive.

    So this time I am replying by writing in a document and pasting it into the web site when I am ready to enter
    my response.

    Thank you for your response. I appreciated most of what you stated.

    In fact, my question was really focused on the issue of objects being passed ByRef or ByVal.

    To make my issue more clear, let me provide an outline of my intentions for the next set of
    Code I will be writing.
    Please Login or Register  to view this content.
    Intentions:
    Sub A is tasked with the responsibility of transcribing (no change) data from a worksheet (“GarageRoom”)
    into an object, aRoom. Because the data is substantially different on the worksheet for Features B,C,D, I
    will be writing three subroutines to handle the transcribing of three very different types of data organization.

    Issues:
    As you can see, I wrote the above pseudo code in a manner in which the worksheet is passed ByVal as I did
    Not want to alter the worksheet data and it seems the acceptable way to prevent changing of original data
    in VBA is to pass an object ByVal (copy). It seems significantly wasteful to copy, four times, a worksheet that
    has extensive data within it. Imagine a building having 244 rooms. That means the worksheet would have to be
    copied 976 times.

    So if such a number of copies is actually a problem, then the pursuit becomes finding ways to eliminate that
    concern. I have imagined two possibilities:
    • Make worksheet a read only worksheet. Refer to it ByRef. Although VBA may allow worksheet data to be changed
    The worksheet object will prevent such action. I would prefer to find a means to prevent changing data in a worksheet
    By code, but maybe that is not possible.
    • A second possibility is to violate my goal of avoiding global variables. Only one of four subroutines can change
    this specific worksheet data and so troubleshooting may be manageable. I am assuming I would declare and create
    A pointer to the worksheet variable and put it on the top of the module of code holding sub A and its calls to Sub B,C,D.
    While providing worksheet scope in the above manner seems manageable, I am worried because my understanding
    Is that when I make something global, it is global to ALL modules of code including one that made the original
    Call to Sub A…. Hence troubleshooting might quickly become impossible. I should mention, I am aware that when a
    subroutine or function terminates, the variables within that subroutine or function are no longer available to any other
    subroutine or function. But if the module of code remains open, and other modules remain open, then suddenly do not
    I have a potential problem with a global variable within a module being exposed to all open modules?

    I welcome any comments. L wish my ignorance were less but so be it. I know what I know and don’t know what I don’t know.
    Bil

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: behind the scenes in passing byref or byVal

    Quote Originally Posted by whburling View Post
    In fact, my question was really focused on the issue of objects being passed ByRef or ByVal.
    Before I even read and think about and respond to the rest of this let me address this one sentence.

    An object variable is itself a pointer that points to the state of the object. So if you pass an object ByVal, you are passing a copy of the pointer, but it is still pointing to the same object as the caller. This is completely different than how passing scalar variables works.

    With regard to your original statement
    It is commonly explained that passing an object ByRef passes a pointer to an object and passing an object ByVal passes a copy of the object.
    This is 100% incorrect.

    Here is an illustration of what happens if you pass an object by value and by reference.

    Module code for class IntegerContainer:
    Please Login or Register  to view this content.
    Test code:
    Please Login or Register  to view this content.
    Output of test code:
    Please Login or Register  to view this content.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: behind the scenes in passing byref or byVal

    I wrote the above pseudo code in a manner in which the worksheet is passed ByVal as I did
    Not want to alter the worksheet data and it seems the acceptable way to prevent changing of original data
    in VBA is to pass an object ByVal (copy). It seems significantly wasteful to copy, four times, a worksheet that
    has extensive data within it. Imagine a building having 244 rooms. That means the worksheet would have to be
    copied 976 times.
    That is not how it works. A worksheet is an object, so if you pass a Worksheet object variable ByVal, you are just passing a copy of the reference to the worksheet, not a copy of the entire worksheet.

    Also, I would make Sub A a method of Room_Class, and pass in the actual data from the worksheet rather than passing the worksheet itself and allowing A to have knowledge of how the worksheet is organized. That information should be hidden from Sub A.

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: behind the scenes in passing byref or byVal

    Quote Originally Posted by whburling View Post
    It is commonly explained that passing an object ByRef passes a pointer to an object and passing an object ByVal passes a copy of the object.
    Not a copy of the object itself, but a different pointer to it. That simply means that any changes made by the called procedure are still made to the object, but you cannot make the original pointer refer to a different object. So, for example, if you pass a ByRef variable that refers to a worksheet, the called routine can both alter the worksheet itself and change the passed variable to refer to a different worksheet and that will affect the calling procedure too. If you pass the same variable ByVal, the called code can still change the worksheet itself and can make the pointer refer to a different sheet but that will not affect the calling code.

    For example:

    Please Login or Register  to view this content.
    This is why event procedures pass objects byval - there can be multiple sinks for the event(s) of a given object and they should all work with the same object arguments.
    Rory

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Passing variable to Word BYRef argument Type Mismatch
    By pjwhitfield in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-19-2018, 11:47 AM
  2. passing object byVal or byref
    By alexba in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-07-2016, 06:50 PM
  3. Call ByVal ByRef StrPtr Address. Please Run a Code for me and post me the results.
    By Doc.AElstein in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-06-2016, 11:58 AM
  4. UDT passing byref works in WinXP Excel 2010 fails on Win7 64 bit Excel 2010 32 bit
    By MarkInKeizer in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-09-2014, 11:49 AM
  5. [SOLVED] Learning differences between ByVal and ByRef
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-24-2013, 07:48 PM
  6. [SOLVED] ByRef argument type mismatch / Passing array as a function argument
    By pzling in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-29-2012, 06:23 PM
  7. Error passing element of string array get ByRef argument type mismatch
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-17-2011, 02:59 PM

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