Event Arguments : ByRef or ByVal (Which Best)? 
Author Message
 Event Arguments : ByRef or ByVal (Which Best)?

When I declare an event I want to include an argument to return the object
that raised the event, for example:

  Public Class MyClass
      Public Event MyEvent( [ByXXX]  oSource As MyClass)
  End Class

What I am unsure about is whether it's best to declare the argument as
'ByVal' or 'ByRef'.  Obviously I want to manipulate the original object so
'ByRef' seems to be correct, but I think passing an object with 'ByVal'
would have the same effect (it's not going to be a completely new instance
of 'MyClass' is it?).

The other main concern is which is most performant?  Is there a clear
downside to 'ByRef' in this circumstance?

Any clues here would be much appreciated....thanks.
--
Phil (New Zealand - Aotearoa)



Wed, 20 Oct 2004 05:56:44 GMT  
 Event Arguments : ByRef or ByVal (Which Best)?
Passing ByVal you are passing the copy of your object, so all changes you
make won't be returned to the original object
Vlad


Quote:
> When I declare an event I want to include an argument to return the object
> that raised the event, for example:

>   Public Class MyClass
>       Public Event MyEvent( [ByXXX]  oSource As MyClass)
>   End Class

> What I am unsure about is whether it's best to declare the argument as
> 'ByVal' or 'ByRef'.  Obviously I want to manipulate the original object so
> 'ByRef' seems to be correct, but I think passing an object with 'ByVal'
> would have the same effect (it's not going to be a completely new instance
> of 'MyClass' is it?).

> The other main concern is which is most performant?  Is there a clear
> downside to 'ByRef' in this circumstance?

> Any clues here would be much appreciated....thanks.
> --
> Phil (New Zealand - Aotearoa)



Wed, 20 Oct 2004 10:25:33 GMT  
 Event Arguments : ByRef or ByVal (Which Best)?
Hi Vlad,

actually that depends on the type of object. If it is a value type then that is
correct. If it is a reference type, then a copy is not made.

Passing a reference type (an object created from a class) Byval is the most
performant of all options.  The receiving code works with the same object, all
changes are made on the actual object. The receiving code cannot change the object
pointed to by the originator.

Passing a reference type ByRef is as above, except that it involves an extra step
of dereferencing, and the receiver can actually change the object that the
originator pointed to (rarely a good design practice !)

Passing a value type (an object created fro ma structure) ByVal causes a copy to
be made, so any changes are not reflected back to the originator

Passing a value type ByRef is a strange one <G>  I need to check on this. I think
it use to be the value type would still be copied, but copied back when the called
method exits.  May vary depending on the boundaries.


Quote:
> Passing ByVal you are passing the copy of your object, so all changes you
> make won't be returned to the original object
> Vlad



> > When I declare an event I want to include an argument to return the object
> > that raised the event, for example:

> >   Public Class MyClass
> >       Public Event MyEvent( [ByXXX]  oSource As MyClass)
> >   End Class

> > What I am unsure about is whether it's best to declare the argument as
> > 'ByVal' or 'ByRef'.  Obviously I want to manipulate the original object so
> > 'ByRef' seems to be correct, but I think passing an object with 'ByVal'
> > would have the same effect (it's not going to be a completely new instance
> > of 'MyClass' is it?).

> > The other main concern is which is most performant?  Is there a clear
> > downside to 'ByRef' in this circumstance?

> > Any clues here would be much appreciated....thanks.
> > --
> > Phil (New Zealand - Aotearoa)



Wed, 20 Oct 2004 13:05:35 GMT  
 Event Arguments : ByRef or ByVal (Which Best)?

Okay, passing a value type ByRef looks like it just passes the variable's address.


Quote:
> Hi Vlad,

> actually that depends on the type of object. If it is a value type then that is
> correct. If it is a reference type, then a copy is not made.

> Passing a reference type (an object created from a class) Byval is the most
> performant of all options.  The receiving code works with the same object, all
> changes are made on the actual object. The receiving code cannot change the
object
> pointed to by the originator.

> Passing a reference type ByRef is as above, except that it involves an extra
step
> of dereferencing, and the receiver can actually change the object that the
> originator pointed to (rarely a good design practice !)

> Passing a value type (an object created fro ma structure) ByVal causes a copy to
> be made, so any changes are not reflected back to the originator

> Passing a value type ByRef is a strange one <G>  I need to check on this. I
think
> it use to be the value type would still be copied, but copied back when the
called
> method exits.  May vary depending on the boundaries.



> > Passing ByVal you are passing the copy of your object, so all changes you
> > make won't be returned to the original object
> > Vlad



> > > When I declare an event I want to include an argument to return the object
> > > that raised the event, for example:

> > >   Public Class MyClass
> > >       Public Event MyEvent( [ByXXX]  oSource As MyClass)
> > >   End Class

> > > What I am unsure about is whether it's best to declare the argument as
> > > 'ByVal' or 'ByRef'.  Obviously I want to manipulate the original object so
> > > 'ByRef' seems to be correct, but I think passing an object with 'ByVal'
> > > would have the same effect (it's not going to be a completely new instance
> > > of 'MyClass' is it?).

> > > The other main concern is which is most performant?  Is there a clear
> > > downside to 'ByRef' in this circumstance?

> > > Any clues here would be much appreciated....thanks.
> > > --
> > > Phil (New Zealand - Aotearoa)



Wed, 20 Oct 2004 13:37:08 GMT  
 Event Arguments : ByRef or ByVal (Which Best)?
Hi Bill.
I'm little bit confused with your explanation.
Could you please provide a sample for ByVal argument whith changes returned
to the original object.
Thank you
Vlad


Quote:
> Hi Vlad,

> actually that depends on the type of object. If it is a value type then
that is
> correct. If it is a reference type, then a copy is not made.

> Passing a reference type (an object created from a class) Byval is the
most
> performant of all options.  The receiving code works with the same object,
all
> changes are made on the actual object. The receiving code cannot change
the object
> pointed to by the originator.

> Passing a reference type ByRef is as above, except that it involves an
extra step
> of dereferencing, and the receiver can actually change the object that the
> originator pointed to (rarely a good design practice !)

> Passing a value type (an object created fro ma structure) ByVal causes a
copy to
> be made, so any changes are not reflected back to the originator

> Passing a value type ByRef is a strange one <G>  I need to check on this.
I think
> it use to be the value type would still be copied, but copied back when
the called
> method exits.  May vary depending on the boundaries.



> > Passing ByVal you are passing the copy of your object, so all changes
you
> > make won't be returned to the original object
> > Vlad



> > > When I declare an event I want to include an argument to return the
object
> > > that raised the event, for example:

> > >   Public Class MyClass
> > >       Public Event MyEvent( [ByXXX]  oSource As MyClass)
> > >   End Class

> > > What I am unsure about is whether it's best to declare the argument as
> > > 'ByVal' or 'ByRef'.  Obviously I want to manipulate the original
object so
> > > 'ByRef' seems to be correct, but I think passing an object with
'ByVal'
> > > would have the same effect (it's not going to be a completely new
instance
> > > of 'MyClass' is it?).

> > > The other main concern is which is most performant?  Is there a clear
> > > downside to 'ByRef' in this circumstance?

> > > Any clues here would be much appreciated....thanks.
> > > --
> > > Phil (New Zealand - Aotearoa)



Wed, 20 Oct 2004 19:33:01 GMT  
 Event Arguments : ByRef or ByVal (Which Best)?

Quote:
> I'm little bit confused with your explanation.
> Could you please provide a sample for ByVal argument whith
> changes returned to the original object.

No matter, if the argument is a reference type or a value type:

- ByVal: The argument is a copy of the content of the passed variable =>
changing the argument does not change the content of the passed variable.

- ByRef: The argument is a pointer to the passed variable => changing the
argument changes the content of the passed variable.

If the argument is a reference type:
Changes to the object referenced by the argument are changes to the object,
no matter if ByVal or ByRef is used.

Put the following code in a new windows application (only contains Form1):

   Public AnyProp As Integer

   Private Sub Form1_Load( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles MyBase.Load

      Dim ValueType As Integer
      Dim ReferenceType As Form1

      ValueType = 1
      ValueTypeByVal(ValueType)
      Debug.WriteLine("ValueTypeByVal: " & ValueType)

      ValueType = 1
      ValueTypeByRef(ValueType)
      Debug.WriteLine("ValueTypeByRef: " & ValueType)

      ReferenceType = Me
      ReferenceTypeByVal(ReferenceType)
      Debug.WriteLine("ReferenceTypeByVal: " & (ReferenceType Is Me))

      ReferenceType = Me
      ReferenceTypeByRef(ReferenceType)
      Debug.WriteLine("ReferenceTypeByRef: " & (ReferenceType Is Me))

      ReferenceType = New Form1()
      ReferenceTypeContentByVal(ReferenceType)
      Debug.WriteLine("ReferenceTypeContentByVal: " & ReferenceType.AnyProp)

      ReferenceType = New Form1()
      ReferenceTypeContentByRef(ReferenceType)
      Debug.WriteLine("ReferenceTypeContentByRef: " & ReferenceType.AnyProp)

   End Sub

   Private Sub ValueTypeByVal(ByVal Value As Integer)
      Value += 1
   End Sub
   Private Sub ValueTypeByRef(ByRef Value As Integer)
      Value += 1
   End Sub
   Private Sub ReferenceTypeByVal(ByVal Value As Form1)
      Value = Nothing
   End Sub
   Private Sub ReferenceTypeByRef(ByRef Value As Form1)
      Value = Nothing
   End Sub
   Private Sub ReferenceTypeContentByVal(ByVal Value As Form1)
      Value.AnyProp += 1
   End Sub
   Private Sub ReferenceTypeContentByRef(ByRef Value As Form1)
      Value.AnyProp += 1
   End Sub

Armin



Wed, 20 Oct 2004 21:54:57 GMT  
 Event Arguments : ByRef or ByVal (Which Best)?
Armin,
Thanks a lot for clarification. You are absolutely right
Vlad


Quote:

> > I'm little bit confused with your explanation.
> > Could you please provide a sample for ByVal argument whith
> > changes returned to the original object.

> No matter, if the argument is a reference type or a value type:

> - ByVal: The argument is a copy of the content of the passed variable =>
> changing the argument does not change the content of the passed variable.

> - ByRef: The argument is a pointer to the passed variable => changing the
> argument changes the content of the passed variable.

> If the argument is a reference type:
> Changes to the object referenced by the argument are changes to the
object,
> no matter if ByVal or ByRef is used.

> Put the following code in a new windows application (only contains Form1):

>    Public AnyProp As Integer

>    Private Sub Form1_Load( _
>       ByVal sender As System.Object, _
>       ByVal e As System.EventArgs) _
>       Handles MyBase.Load

>       Dim ValueType As Integer
>       Dim ReferenceType As Form1

>       ValueType = 1
>       ValueTypeByVal(ValueType)
>       Debug.WriteLine("ValueTypeByVal: " & ValueType)

>       ValueType = 1
>       ValueTypeByRef(ValueType)
>       Debug.WriteLine("ValueTypeByRef: " & ValueType)

>       ReferenceType = Me
>       ReferenceTypeByVal(ReferenceType)
>       Debug.WriteLine("ReferenceTypeByVal: " & (ReferenceType Is Me))

>       ReferenceType = Me
>       ReferenceTypeByRef(ReferenceType)
>       Debug.WriteLine("ReferenceTypeByRef: " & (ReferenceType Is Me))

>       ReferenceType = New Form1()
>       ReferenceTypeContentByVal(ReferenceType)
>       Debug.WriteLine("ReferenceTypeContentByVal: " &

ReferenceType.AnyProp)
Quote:

>       ReferenceType = New Form1()
>       ReferenceTypeContentByRef(ReferenceType)
>       Debug.WriteLine("ReferenceTypeContentByRef: " &

ReferenceType.AnyProp)

- Show quoted text -

Quote:

>    End Sub

>    Private Sub ValueTypeByVal(ByVal Value As Integer)
>       Value += 1
>    End Sub
>    Private Sub ValueTypeByRef(ByRef Value As Integer)
>       Value += 1
>    End Sub
>    Private Sub ReferenceTypeByVal(ByVal Value As Form1)
>       Value = Nothing
>    End Sub
>    Private Sub ReferenceTypeByRef(ByRef Value As Form1)
>       Value = Nothing
>    End Sub
>    Private Sub ReferenceTypeContentByVal(ByVal Value As Form1)
>       Value.AnyProp += 1
>    End Sub
>    Private Sub ReferenceTypeContentByRef(ByRef Value As Form1)
>       Value.AnyProp += 1
>    End Sub

> Armin



Wed, 20 Oct 2004 22:14:33 GMT  
 Event Arguments : ByRef or ByVal (Which Best)?
That's a massively useful rundown on the various argument type permutations.

Quote:
>>  and the receiver can actually change the object that the
>>  originator pointed to (rarely a good design practice !)

I hear your point there, giving all and sundry event handlers the ability to
change the event source could/would be disastrous!

Thanks for you insights Bill.
--
Phil (New Zealand - Aotearoa)


Quote:

> Okay, passing a value type ByRef looks like it just passes the variable's
address.



> > Hi Vlad,

> > actually that depends on the type of object. If it is a value type then
that is
> > correct. If it is a reference type, then a copy is not made.

> > Passing a reference type (an object created from a class) Byval is the
most
> > performant of all options.  The receiving code works with the same
object, all
> > changes are made on the actual object. The receiving code cannot change
the
> object
> > pointed to by the originator.

> > Passing a reference type ByRef is as above, except that it involves an
extra
> step
> > of dereferencing, and the receiver can actually change the object that
the
> > originator pointed to (rarely a good design practice !)

> > Passing a value type (an object created fro ma structure) ByVal causes a
copy to
> > be made, so any changes are not reflected back to the originator

> > Passing a value type ByRef is a strange one <G>  I need to check on
this. I
> think
> > it use to be the value type would still be copied, but copied back when
the
> called
> > method exits.  May vary depending on the boundaries.



> > > Passing ByVal you are passing the copy of your object, so all changes
you
> > > make won't be returned to the original object
> > > Vlad



> > > > When I declare an event I want to include an argument to return the
object
> > > > that raised the event, for example:

> > > >   Public Class MyClass
> > > >       Public Event MyEvent( [ByXXX]  oSource As MyClass)
> > > >   End Class

> > > > What I am unsure about is whether it's best to declare the argument
as
> > > > 'ByVal' or 'ByRef'.  Obviously I want to manipulate the original
object so
> > > > 'ByRef' seems to be correct, but I think passing an object with
'ByVal'
> > > > would have the same effect (it's not going to be a completely new
instance
> > > > of 'MyClass' is it?).

> > > > The other main concern is which is most performant?  Is there a
clear
> > > > downside to 'ByRef' in this circumstance?

> > > > Any clues here would be much appreciated....thanks.
> > > > --
> > > > Phil (New Zealand - Aotearoa)



Thu, 21 Oct 2004 06:10:50 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Pass arguments ByVal or ByRef

2. Good Example for ByRef, ByVal

3. calling an event with byval object argument

4. Property Let argument ByRef is NOT ByRef ?

5. Override ByRef with ByVal

6. ByVal / ByRef

7. byVal vs ByRef

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

9. Change Standard from ByVal to ByRef

10. Peformance in VB.NET - ByRef and ByVal

11. objects byval and byref

12. ByRef And ByVal for Object

 

 
Powered by phpBB® Forum Software