byVal vs ByRef 
Author Message
 byVal vs ByRef

Hello
I understand what each of these do (ByVal and ByRef), but can anyone give me
an example when ByVal would be used?

BTW... would it be ok for me to post similar questions in the future?
Conceptual questions.

Thank you so much
Terry V



Sat, 24 Sep 2005 01:09:55 GMT  
 byVal vs ByRef
Hi, Terry,

Yes, this is the perfect place for questions like this. I hope that
collectively we have the answers! <g>

I can think of a couple of circumstances where you'd use ByVal:

One is where you're calling a function or sub that someone else wrote, which
declares its parameters with ByVal. Then you *must* match the declaration.
The two main classes of procedures like this are Win32 API calls and VBA
event procedures. (For a thoroughly confusing discussion of the latter, see
the topic in VBA Help titled "ByVal References in Microsoft Forms".)

The other is where, as a defensive programming tactic, you want to make sure
that a procedure you're writing doesn't alter the values in the calling
procedure, even though you may change them locally. You may or may not
intend to change the local values, but declaring ByVal ensures that nothing
"leaks". A subset of this category might be certain recursive routines,
where you need to be sure that each iteration has its own set of values.

--
Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://www.mvps.org/word

Quote:

> Hello
> I understand what each of these do (ByVal and ByRef), but can anyone
> give me an example when ByVal would be used?

> BTW... would it be ok for me to post similar questions in the future?
> Conceptual questions.

> Thank you so much
> Terry V



Sat, 24 Sep 2005 03:28:35 GMT  
 byVal vs ByRef
Thank you Jay
With the ByVal,
EX:  If I had a sub that called on more than 1 procedure and I needed the
variable value to be its original value for each of the procedures, I would
use the ByVal.  right?  LOL
EX:

Public tot as Long
Public cost as Long
Public sub test1(ByVal lngValue as Long)
Dim x as Integer
Dim y as Integer
Dim z as Integer
x=10
y=20
Call Test2(x,y)
z = x / tot
calc(x)
thecost = cost
msgbox "The Cost is" & thecost & vbcrlf _
    & "The fraction is " & z
End sub

Public function Test2(ByVal no1, ByRef no2)
tot=no1*no2
End function

Public sub calc(ref1)
cost = ref1 * 100
End sub

Here Im not sure why z calculates to 0, but do I have the right concept?

Thanks so much Jay....


Quote:
> Hi, Terry,

> Yes, this is the perfect place for questions like this. I hope that
> collectively we have the answers! <g>

> I can think of a couple of circumstances where you'd use ByVal:

> One is where you're calling a function or sub that someone else wrote,
which
> declares its parameters with ByVal. Then you *must* match the declaration.
> The two main classes of procedures like this are Win32 API calls and VBA
> event procedures. (For a thoroughly confusing discussion of the latter,
see
> the topic in VBA Help titled "ByVal References in Microsoft Forms".)

> The other is where, as a defensive programming tactic, you want to make
sure
> that a procedure you're writing doesn't alter the values in the calling
> procedure, even though you may change them locally. You may or may not
> intend to change the local values, but declaring ByVal ensures that
nothing
> "leaks". A subset of this category might be certain recursive routines,
> where you need to be sure that each iteration has its own set of values.

> --
> Regards,
> Jay Freedman
> Microsoft Word MVP          FAQ: http://www.mvps.org/word


> > Hello
> > I understand what each of these do (ByVal and ByRef), but can anyone
> > give me an example when ByVal would be used?

> > BTW... would it be ok for me to post similar questions in the future?
> > Conceptual questions.

> > Thank you so much
> > Terry V



Sat, 24 Sep 2005 04:53:30 GMT  
 byVal vs ByRef
Hi Terry

I'd like to amplify on Jay's comments.

I NEVER use ByRef unless I absolutely have to. It's like missing out the
Option Explicit statement in your module - it leads to all kinds of
unexpected errors. This occurs because most of us treat variables defined
within procedures as local in scope and we often extend this definition to
include the procedures argument list. I ALWAYS explicitly declare every
argument either ByVal or ByRef, then there's no ambiguity about what my
intention is.

You rarely need to use ByRef, I write lots of code and the only ByRef I've
used in the last couple of months is in a recursive Sort procedure.

BTW - Passing in objects (as opposed to native VBA data types) ByVal does
not protect the object as what's being passed to the procedure is just an
object pointer. So the pointer is protected but not the object.

There are different opinions out there as declaring arguments ByVal causes
VBA to copy the entire data structure so that your procedure has it's own
local copy. Obviously if the structure your passing in is large or you're
calling the procedure in a tight loop then there are overheads.

I hope this helps + Cheers - Peter



Quote:

> Subject: Re: byVal vs ByRef
> Newsgroups: microsoft.public.word.vba.general

> Hi, Terry,

> Yes, this is the perfect place for questions like this. I hope that
> collectively we have the answers! <g>

> I can think of a couple of circumstances where you'd use ByVal:

> One is where you're calling a function or sub that someone else wrote,
> which declares its parameters with ByVal. Then you *must* match the
> declaration. The two main classes of procedures like this are Win32 API
> calls and VBA event procedures. (For a thoroughly confusing discussion
> of the latter, see the topic in VBA Help titled "ByVal References in
> Microsoft Forms".)

> The other is where, as a defensive programming tactic, you want to make
> sure that a procedure you're writing doesn't alter the values in the
> calling procedure, even though you may change them locally. You may or
> may not intend to change the local values, but declaring ByVal ensures
> that nothing "leaks". A subset of this category might be certain
> recursive routines, where you need to be sure that each iteration has
> its own set of values.

> --
> Regards,
> Jay Freedman
> Microsoft Word MVP          FAQ: http://www.mvps.org/word


>> Hello
>> I understand what each of these do (ByVal and ByRef), but can anyone
>> give me an example when ByVal would be used?

>> BTW... would it be ok for me to post similar questions in the future?
>> Conceptual questions.

>> Thank you so much
>> Terry V



Sat, 24 Sep 2005 06:01:59 GMT  
 byVal vs ByRef
Hi Terry

Copy and Paste and then run the following code:

Public Sub TestStart()
    Dim lngP1 As Long, lngP2 As Long, lngP3 As Long

    lngP1 = 100
    lngP2 = 200
    lngP3 = 300

    MsgBox "P1 = " & lngP1 & ", P2 = " & lngP2 & ", P3 = " & lngP3
    TestArgPassing lngP1, lngP2, lngP3
    MsgBox "P1 = " & lngP1 & ", P2 = " & lngP2 & ", P3 = " & lngP3
End Sub

Public Sub TestArgPassing(ByVal lngA1 As Long, _
                          ByRef lngA2 As Long, _
                          ByVal lngA3 As Long)
    lngA1 = lngA1 * lngA1
    lngA2 = lngA2 * lngA2
    lngA3 = lngA3 * lngA3
End Sub

You can see that only the value of lngP2 gets changed.

I hope this helps + Cheers - Peter


Quote:
> Thank you Jay
> With the ByVal,
> EX:  If I had a sub that called on more than 1 procedure and I needed the
> variable value to be its original value for each of the procedures, I
would
> use the ByVal.  right?  LOL
> EX:

> Public tot as Long
> Public cost as Long
> Public sub test1(ByVal lngValue as Long)
> Dim x as Integer
> Dim y as Integer
> Dim z as Integer
> x=10
> y=20
> Call Test2(x,y)
> z = x / tot
> calc(x)
> thecost = cost
> msgbox "The Cost is" & thecost & vbcrlf _
>     & "The fraction is " & z
> End sub

> Public function Test2(ByVal no1, ByRef no2)
> tot=no1*no2
> End function

> Public sub calc(ref1)
> cost = ref1 * 100
> End sub

> Here Im not sure why z calculates to 0, but do I have the right concept?



Sat, 24 Sep 2005 06:26:11 GMT  
 byVal vs ByRef
Yes Peter
This does help...
Im in the midst of studying a VBA course be Appdev and because the course
does not give too many examples of use for most of the content and I need
that to learn well, I look for instances of use and explanations
(practical).

So I thank you and Jay so very much
Terry V
====================================================

Quote:
> Hi Terry

> I'd like to amplify on Jay's comments.

> I NEVER use ByRef unless I absolutely have to. It's like missing out the
> Option Explicit statement in your module - it leads to all kinds of
> unexpected errors. This occurs because most of us treat variables defined
> within procedures as local in scope and we often extend this definition to
> include the procedures argument list. I ALWAYS explicitly declare every
> argument either ByVal or ByRef, then there's no ambiguity about what my
> intention is.

> You rarely need to use ByRef, I write lots of code and the only ByRef I've
> used in the last couple of months is in a recursive Sort procedure.

> BTW - Passing in objects (as opposed to native VBA data types) ByVal does
> not protect the object as what's being passed to the procedure is just an
> object pointer. So the pointer is protected but not the object.

> There are different opinions out there as declaring arguments ByVal causes
> VBA to copy the entire data structure so that your procedure has it's own
> local copy. Obviously if the structure your passing in is large or you're
> calling the procedure in a tight loop then there are overheads.

> I hope this helps + Cheers - Peter




> > Subject: Re: byVal vs ByRef
> > Newsgroups: microsoft.public.word.vba.general

> > Hi, Terry,

> > Yes, this is the perfect place for questions like this. I hope that
> > collectively we have the answers! <g>

> > I can think of a couple of circumstances where you'd use ByVal:

> > One is where you're calling a function or sub that someone else wrote,
> > which declares its parameters with ByVal. Then you *must* match the
> > declaration. The two main classes of procedures like this are Win32 API
> > calls and VBA event procedures. (For a thoroughly confusing discussion
> > of the latter, see the topic in VBA Help titled "ByVal References in
> > Microsoft Forms".)

> > The other is where, as a defensive programming tactic, you want to make
> > sure that a procedure you're writing doesn't alter the values in the
> > calling procedure, even though you may change them locally. You may or
> > may not intend to change the local values, but declaring ByVal ensures
> > that nothing "leaks". A subset of this category might be certain
> > recursive routines, where you need to be sure that each iteration has
> > its own set of values.

> > --
> > Regards,
> > Jay Freedman
> > Microsoft Word MVP          FAQ: http://www.mvps.org/word


> >> Hello
> >> I understand what each of these do (ByVal and ByRef), but can anyone
> >> give me an example when ByVal would be used?

> >> BTW... would it be ok for me to post similar questions in the future?
> >> Conceptual questions.

> >> Thank you so much
> >> Terry V



Sat, 24 Sep 2005 09:04:52 GMT  
 byVal vs ByRef
To add my contribution to the story:

Me too, I always pass arguments ByVal unless specific conditions
force to me do differently.

Before .DOTNET, Microsoft defaulted all arguments passed ByRef
unless you make them go in ByVal.

Microsoft has changed this from DOTNET on
The CLR (Common Language Runtime) assumes arguments
passed ByVal rather than ByRef.

Better get used to the idea and adapt to working with ByVal arguments
and more specifically what how they behave.

Krgrds,
Perry



Quote:
> Yes Peter
> This does help...
> Im in the midst of studying a VBA course be Appdev and because the course
> does not give too many examples of use for most of the content and I need
> that to learn well, I look for instances of use and explanations
> (practical).

> So I thank you and Jay so very much
> Terry V
> ====================================================


> > Hi Terry

> > I'd like to amplify on Jay's comments.

> > I NEVER use ByRef unless I absolutely have to. It's like missing out the
> > Option Explicit statement in your module - it leads to all kinds of
> > unexpected errors. This occurs because most of us treat variables
defined
> > within procedures as local in scope and we often extend this definition
to
> > include the procedures argument list. I ALWAYS explicitly declare every
> > argument either ByVal or ByRef, then there's no ambiguity about what my
> > intention is.

> > You rarely need to use ByRef, I write lots of code and the only ByRef
I've
> > used in the last couple of months is in a recursive Sort procedure.

> > BTW - Passing in objects (as opposed to native VBA data types) ByVal
does
> > not protect the object as what's being passed to the procedure is just
an
> > object pointer. So the pointer is protected but not the object.

> > There are different opinions out there as declaring arguments ByVal
causes
> > VBA to copy the entire data structure so that your procedure has it's
own
> > local copy. Obviously if the structure your passing in is large or
you're
> > calling the procedure in a tight loop then there are overheads.

> > I hope this helps + Cheers - Peter




> > > Subject: Re: byVal vs ByRef
> > > Newsgroups: microsoft.public.word.vba.general

> > > Hi, Terry,

> > > Yes, this is the perfect place for questions like this. I hope that
> > > collectively we have the answers! <g>

> > > I can think of a couple of circumstances where you'd use ByVal:

> > > One is where you're calling a function or sub that someone else wrote,
> > > which declares its parameters with ByVal. Then you *must* match the
> > > declaration. The two main classes of procedures like this are Win32
API
> > > calls and VBA event procedures. (For a thoroughly confusing discussion
> > > of the latter, see the topic in VBA Help titled "ByVal References in
> > > Microsoft Forms".)

> > > The other is where, as a defensive programming tactic, you want to
make
> > > sure that a procedure you're writing doesn't alter the values in the
> > > calling procedure, even though you may change them locally. You may or
> > > may not intend to change the local values, but declaring ByVal ensures
> > > that nothing "leaks". A subset of this category might be certain
> > > recursive routines, where you need to be sure that each iteration has
> > > its own set of values.

> > > --
> > > Regards,
> > > Jay Freedman
> > > Microsoft Word MVP          FAQ: http://www.mvps.org/word


> > >> Hello
> > >> I understand what each of these do (ByVal and ByRef), but can anyone
> > >> give me an example when ByVal would be used?

> > >> BTW... would it be ok for me to post similar questions in the future?
> > >> Conceptual questions.

> > >> Thank you so much
> > >> Terry V



Sat, 24 Sep 2005 22:28:29 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. ByVal vs ByRef

2. Byval vs Byref

3. Passing Parameters - A ByRef vs ByVal Question

4. ByVal vs. ByRef -- preformance

5. byval vs. byref

6. ByRef Vs ByVal

7. Override ByRef with ByVal

8. ByVal / ByRef

9. Questions regarding objects, assignments, byval, byref, etc...

10. Change Standard from ByVal to ByRef

11. Event Arguments : ByRef or ByVal (Which Best)?

12. Peformance in VB.NET - ByRef and ByVal

 

 
Powered by phpBB® Forum Software