|
-
Jul 8th, 2005, 03:44 PM
#1
Thread Starter
No place like 127.0.0.1
[RESOLVED] ByVal in Function Arguments
If this:
VB Code:
Private Function MyFunc(ByVal sTest As String)
The same as this:
VB Code:
Private Function MyFunc(sTest As String)
Or does the second one mean ByVal/ByRef and require more CPU time? The reason I ask is that I have quite a few arguments in the function I am making and ByVal infront of all of them makes it very long when the AutoFill text pops up.
-
Jul 8th, 2005, 03:48 PM
#2
Re: ByVal in Function Arguments
ByVal means that the function can change the value of the variable. ByRef means that a new instance is invoked, and destroyed when the function ends.
Might take a bit longer to invoke, but you would also save time not having to copy that variable back after the function ends.
-
Jul 8th, 2005, 03:51 PM
#3
Thread Starter
No place like 127.0.0.1
Re: ByVal in Function Arguments
Thank dglienna, but I need to know what happens if ByVal and ByRef are ommited. It it treated like ByVal automatically or is it somewhat like variant and take up more CPU time?
-
Jul 8th, 2005, 03:53 PM
#4
Re: ByVal in Function Arguments
dglienna: a bit incorrect. ByVal just means that a copy of the variable is passed to the procedure. ByRef means that the original variable is passed to the procedure. This means that when you change a ByRef variable in the procedure (be it a function or a sub), the passed variable changes as there is no copy of the variable. As far as I know, ByVal is slightly slower than ByRef; I've never tested it really (or if I have, I've forgotten the results long time ago).
I think ByRef was used by default. I'm not sure though, I tend to always use ByRef and ByVal so I can be sure of which it does But yes, it is either ByVal or ByRef, nothing other special.
-
Jul 8th, 2005, 03:56 PM
#5
Re: ByVal in Function Arguments
The second example is passing the variable "ByRef" and not ByVal. The default is ByRef when nothing is designated.
Although it may seem easier to not include the ByVal, it actually is a completely different meaning. ByVal is like passing in a
copy of the variable to your function. At the calling line of code the passed value can not be retrieved.
However if you use ByRef then you are only passing a link that represents the variable and any changes to the variable are reflected
back.
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 
-
Jul 8th, 2005, 04:05 PM
#6
Re: ByVal in Function Arguments
byVal is the default in VB.Net
That's a change from VB6 (and prior).
[edit] So specify it (so conversion to .Net is easier)...
-
Jul 8th, 2005, 04:08 PM
#7
Re: ByVal in Function Arguments
Oops. I didn't read it closely enough. The last sentence says can't. Sorry.
by value
A way of passing the value, rather than the address, of an argument to a procedure. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.
Here's byref
by reference
A way of passing the address, rather than the value, of an argument to a procedure. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed.
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
|