Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

byVal and byRef

Status
Not open for further replies.

OhioSteve

MIS
Mar 12, 2002
1,352
US
If I do not put "byVal" or "byRef" in my function signatures, thn VBA assumes that I want "byRef". Is there a way to change that default? Can I put something at the top of my module that makes "byVal" the default?
 
But are you sure that is what you want to do??? Do you know what the ramifications of using ByVal vs ByRef are? Have you looked them up in the help files to see what the two actually mean?

ByRef is typically what you want to use....This "preserves" the original item that you used, whereas ByVal will change it.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Yes, I understand the difference.

When you pass a variable by value, you create a new copy of the variable and pass the copy. If you make changes to that variable in the next function, those changes happen to the copy, not the original. When you return to the original function, the original value is unchanged.

When you pass a variable by reference, you pass the memory address containing the variable. If you make changes to the variable in the next function, those changes are permanent.

 
Alright. As far as I know there is not a way to change this. And my searches did not turn anything up either. Sorry.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
How are ya OhioSteve . . .

Defaulting [blue]By Value[/blue] would have every tendency to create a resource guzzler! This is one other time I have to agree with microsoft. Besides . . . for all my years its been the rare occasion I've had to use [blue]By Value[/blue]!

Calvin.gif
See Ya! . . . . . .
 
One of my MS VB books actually states the opposite - that ByVal should be used, unless you have an explicit reason to pass ByRef.

The book states that passing ByRef creates the potential to implement subtle bugs in your code. This much I can understand.

It also states that passing ByRef when you don't need to actually prevents the compiler from correctly optimising the code. This seems counterintuitive to me, although I have read a detailed explanation of why this is (which made perfect sense at the time, unfortunately I can no longer remember the specifics!). If I can find the article I'll post a link.

Since I read the book and the supporting article I now pass all parameters by value, unless I have a reason not to. It is also interesting to note that versions of Visual Basic post VB6 have all passed parameters by value bye default.

Ed Metcalfe.

Please do not feed the trolls.....
 
Ed2020 said:
[blue]The book states that passing ByRef creates the potential to implement subtle bugs in your code.[/blue]
I disagree. The only so called bug I ever heard of or come to know is that which happens when [purple]a proprammer changes the value of one passed by reference![/purple] Any bugs will certainly be bourne out of this [red]otherwise there's someting wrong with access![/red] [blue]Can you state what some of these subtle bugs are and how they come about if one doesn't change the reference By Value?[/blue]
Ed2020 said:
[blue]It also states that passing ByRef when you don't need to actually prevents the compiler from correctly optimising the code.[/blue]
I disagree even more! Variables and passed values are not optimized but are alloted space in the compile and how alloting space (an explicit function during compile) interfere's I've never read, learned, or come across. [blue]A few lines on how this happens are in order!?[/blue]

[purple] . . . and which book is this? . . . which page?[/purple]

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .
 
TheAceMan1 said:
I disagree. The only so called bug I ever heard of or come to know is that which happens when a proprammer changes the value of one passed by reference! Any bugs will certainly be bourne out of this otherwise there's someting wrong with access! Can you state what some of these subtle bugs are and how they come about if one doesn't change the reference By Value?

I assume the example you describe was the potential source of bugs to which the author was referring - the programmer inadvertently altering a parameter passed by reference and then making use of the amended value in the calling procedure, rather than bugs in Access/VBA/VB6. Passing by value avoids this specific issue.

If this was not what the author was alluding to then no, I can't state what some of these subtle bugs are - it is not an assertion I am making. You would need to contact the author.

TheAceMan1 said:
I disagree even more! Variables and passed values are not optimized but are alloted space in the compile and how alloting space (an explicit function during compile) interfere's I've never read, learned, or come across. A few lines on how this happens are in order!?

"Optimised" may be the wrong use of word - it was however the word used in the book. My understanding is that passing by value is, in many circumstances, faster than passing by reference. This is backed up by page 758 of the VB6 Programmer's Guide, plus a number of pages I have read on the Internet with real world tests. I am again making the assumption that this is the "optimisation" the author was referring to.

TheAceMan1 said:
. . . and which book is this? . . . which page?

Programming Microsoft Visual Basic .NET; page 66. Having re-read this section I acknowledge that the first paragraph of my previous email was incorrect - this particular book does not explicitly say that you should use ByVal unless you have a specific reason to pass ByRef. Page 758 of the VB6 Programmer's Guide does, however.

Ed Metcalfe.


Please do not feed the trolls.....
 
Ed2020 . . .

Thanks for getting back and quoting the references. Yes . . . they do say these things! . . . why will remain a mystery to me.

For myself the key here is [blue]simply knowing the difference.[/blue] The purpose of [blue]Pass By Val[/blue] is getting a copy so the origional is unaffected . . . Thats it!

As far as speed is concerned . . . you tell me . . . is it faster to [blue]pass a pointer[/blue] or [purple]pass a value[/purple] who's type and length has to be accounted for (let alone passing objects). This makes [blue]By Ref faster[/blue] but its academic as these microseconds of time will never be felt!

So were again left with our use of these functions. If you decide you need [blue]By Val[/blue] by all means, be my guest! This is an area where we simply have to know what were doing! . . .

Calvin.gif
See Ya! . . . . . .
 
TheAceMan1,

I think it depends on the type of parameter. If passing a scalar ByVal is faster (at least according to the results I have read), if passing a string (for example) ByRef is faster. I have read the logic behind this, but cannot remember where (or indeed what the logic was!).

At some point in history the time differences would have mattered but, as you say, the difference is so miniscule it is irrelevant now (unless perhaps you are calling a sub/function from within a loop?).

TheAceMan1 said:
is it faster to pass a pointer or pass a value who's type and length has to be accounted for (let alone passing objects).

Aren't objects always passed by reference? Or am I thinking of another language.....?

Ed Metcalfe.

Please do not feed the trolls.....
 
Ed2020 . . .

Ever tried it? I've passed enough Recordset Objects [blue]By Val[/blue] in my time . . .

Calvin.gif
See Ya! . . . . . .
 
Wow! I had not expected to inspire such a spirited discussion. In school I was taught that passing by value is the norm. However, I have no aversion to byRef.

In vba you can do stuff like put "option explicit" at the top of the module to change default typing. I just wondered whether something like that exists for byRef/byVal.

 
Ed2020 . . .

Retract my last post (don't know what I was thinking! Yes objects are passed by ref but take more time!

Calvin.gif
See Ya! . . . . . .
 
OhioSteve . . .

Wondered where you ran off too. We're just chewin the fat! . . .

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top