Results 1 to 12 of 12

Thread: Debate? ByRef/ByVal

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Debate? ByRef/ByVal

    Well I read and re-read
    http://www.vbforums.com/showthread.p...ighlight=Byref
    http://www.developerfusion.co.uk/show/51/6/
    and stuff like
    Code:
    It is more efficient to code 
         SUB WriteLine(ByVal L AS STRING)
    than to code
         SUB WriteLine(L AS STRING)
    and it is never necessary to code
         SUB WriteLine(ByRef L AS STRING)
    since that is the default.
    But efficiency is seldom a problem (never so far for me) on modern computers. If a case arose, I would probably opt for avoiding the SUB entirely to omit all overhead.

    Maybe one could use ByVal to use the fact that you are guaranteed that your input variable is not changed.
    y = F1(x) + F2(x)
    for example. If F1 modified x, then F2 would return an unexpected result.

    But if one were so careful as to do that, one could simply not modify x.

    However, if the default were reversed so that all SUBs are "efficient" and passed variables never get changed, then I can imagine a case where one would need to know about ByRef in the case that one actually wanted to return multiple values.

    That not being the case, I think the answer to the question "Why ever use ByVal or ByRef?" is "There is seldom or never a good reason. Forget you ever heard the terms."

    Mac

  2. #2

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Debate? ByRef/ByVal

    Moved
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Debate? ByRef/ByVal

    This is a VB6 issue. By the time you get to .NET, the rules for default have changed, and you really should know the meaning and purpose of ByRef vs. ByVal.
    My usual boring signature: Nothing

  5. #5

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Debate? ByRef/ByVal

    Quote Originally Posted by Mr.Mac
    That not being the case, I think the answer to the question "Why ever use ByVal or ByRef?" is "There is seldom or never a good reason. Forget you ever heard the terms."
    I am not sure how you arrived at that conclusion.
    Passing by value and by reference each have their own purpose, and, like any other language facility, should be used as appropriate.

    In most languages, passing by value is the default argument mode, and the expected behaviour. If passing by reference is required, it must be denoted as such, to avoid surprises.
    In languages such as VB6, the opposite is true, but the principle is equally applicable.

    If you care about efficiency, then you need to care about what platform you're using, and the specifics of the calling convention employed. Generally, passing value types (primitives) by value is more efficient, since they're internally stored as values; passing reference types by value is less efficient than passing them by reference, since they're stored as references.


    You should note, though, that all passing a reference type by value will do is make a copy of the reference; it won't actually clone the object. The function you pass the reference to will still be able to modify the object's state, but it won't be able to modify your original reference.


    Quote Originally Posted by Mr.Mac
    But if one were so careful as to do that, one could simply not modify x.
    Noble. However, it's just as much what you should and shouldn't do as what you can and can't do. "Simply not modifying x" falls over when you have more than one person coding. Plus, self-documenting code is good.

  7. #7
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Debate? ByRef/ByVal

    ByRef is the preferred method for passing strings. It's much faster than passing by value if they are large.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Debate? ByRef/ByVal

    Quote Originally Posted by Ellis Dee
    It's much faster
    I'd like to see a program where it makes a difference.

    True, 0.0000034 is much faster than 0.0000009. But the human cannot see to that level of detail.

    If I enter a word to be translated to Danish

    w$=D2E("mulig")

    No one can detect the ByThis/ByThat nonsense. Whether it figures it out instantly or takes 2 hours, the ByThis/ByThat nonsense will not be a factor.

    IMHO

    (w$="possible")

    Mac

  9. #9
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: Debate? ByRef/ByVal

    Why would you add the ByVal keyword? To protect the original object, right?
    ... The String class is immutable! So, there's no need to protect it.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  10. #10
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: Debate? ByRef/ByVal

    Quote Originally Posted by Mr.Mac
    I'd like to see a program where it makes a difference.

    True, 0.0000034 is much faster than 0.0000009. But the human cannot see to that level of detail.

    If I enter a word to be translated to Danish

    w$=D2E("mulig")

    No one can detect the ByThis/ByThat nonsense. Whether it figures it out instantly or takes 2 hours, the ByThis/ByThat nonsense will not be a factor.

    IMHO

    (w$="possible")

    Mac
    It's a matter of principle.

    That string instance could be a 4MB XML file. If you are going to pass that from one method to the other. Then, it's really a shame to do it ByVal.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Debate? ByRef/ByVal

    In general, when you pass an object by value, you are pushing the entire object onto the stack. When you pass by ref, you are pushing the memory address only.

    While it is true that you can do one or the other, or both, and it generally doesn't make a noticeable difference in speed, speed shouldn't be the reason you are deciding which to do. You can pass things ByRef to use as callbacks, or return results (lots of API calls do this, as well as several TCP/IP operations, TryParse, and many more). This isn't done for speed, it's done because ByVal simply wouldn't work. Alternatively, you can pass ByVal if you will be altering the value, and don't want side effects.

    Simply choose the one that is correct.

    As for speed, perhaps all your programs work in the blink of an eye. I have a deeply iterative program that takes about three days to complete a calculation on a fairly small data set. I don't know how slow it would be on a large dataset, but a week or two would be likely. For something like that, anything that sped up a single interation would have a HUGE impact on total run time.
    My usual boring signature: Nothing

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Debate? ByRef/ByVal

    Quote Originally Posted by BramVandenbon
    Why would you add the ByVal keyword? To protect the original object, right?
    ... The String class is immutable! So, there's no need to protect it.
    Quote Originally Posted by BramVandenbon
    It's a matter of principle.

    That string instance could be a 4MB XML file. If you are going to pass that from one method to the other. Then, it's really a shame to do it ByVal.
    I assume you are referring to .NET strings, which are actually reference types and not value types.

    Quote Originally Posted by penagate
    You should note, though, that all passing a reference type by value will do is make a copy of the reference; it won't actually clone the object.

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