|
-
Jun 10th, 2007, 03:46 PM
#1
Thread Starter
Hyperactive Member
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
-
Jun 10th, 2007, 03:49 PM
#2
Re: Debate? ByRef/ByVal
It is not about "efficiency" - it's all about whether or not original value needs to be preserved. If so you'd use ByVal - that's all.
-
Jun 10th, 2007, 04:08 PM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 10th, 2007, 04:40 PM
#4
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
 
-
Jun 10th, 2007, 04:46 PM
#5
Re: Debate? ByRef/ByVal
How is it an issue? Also, environment isn't matter - the "meaning and purpose" are still the same.
It was not created for VB6 nor it was for VB.Net.
-
Jun 11th, 2007, 01:46 AM
#6
Re: Debate? ByRef/ByVal
 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.
 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.
-
Jun 11th, 2007, 12:46 PM
#7
Re: Debate? ByRef/ByVal
ByRef is the preferred method for passing strings. It's much faster than passing by value if they are large.
-
Jun 11th, 2007, 06:53 PM
#8
Thread Starter
Hyperactive Member
Re: Debate? ByRef/ByVal
 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
-
Jun 11th, 2007, 07:02 PM
#9
Hyperactive Member
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
-
Jun 11th, 2007, 07:09 PM
#10
Hyperactive Member
Re: Debate? ByRef/ByVal
 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
-
Jun 11th, 2007, 07:21 PM
#11
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
 
-
Jun 11th, 2007, 11:37 PM
#12
Re: Debate? ByRef/ByVal
 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.
 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.
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|