Home All Groups Group Topic Archive Search About

default in .NET byref or byval?

Author
26 Jun 2006 4:00 PM
prefersgolfing
In 6, by default arguments, were passed byref what is the default in .NET?
Can you point me to any MSDN documentation? TIA

Author
26 Jun 2006 4:10 PM
Sanjib Biswas
In VB.Net, the arguments in the function/sub are assumed as ByVal unless
explicitly mentioned as ByRef. If you open a VB project under VB.Net and
create a function/sub and only specify argument type and name. After you
save the project you should see that the 'ByVal' has been added to argument.

Regards
Sanjib

Show quoteHide quote
"prefersgolfing" <pregfersgolf***@hotmail.com> wrote in message
news:eBo0tmTmGHA.492@TK2MSFTNGP05.phx.gbl...
> In 6, by default arguments, were passed byref what is the default in .NET?
> Can you point me to any MSDN documentation? TIA
>
Author
26 Jun 2006 4:18 PM
Claes Bergefall
Default is ByVal. See the Remarks (Rules) section here:
http://msdn2.microsoft.com/en-us/library/cbs7z96t.aspx

    /claes

Show quoteHide quote
"prefersgolfing" <pregfersgolf***@hotmail.com> wrote in message
news:eBo0tmTmGHA.492@TK2MSFTNGP05.phx.gbl...
> In 6, by default arguments, were passed byref what is the default in .NET?
> Can you point me to any MSDN documentation? TIA
>
Author
26 Jun 2006 11:30 PM
Dennis
Be careful when passing reference type arguments like a Class By Value.  It's
a little confusing but when these arguments are passed by Value, actually
only a pointer to the argument will be passed and if you change any of the
Class's properties in the sub/funciton, the change will be reflected in the
calling program's instance of the class.
--
Dennis in Houston


Show quoteHide quote
"prefersgolfing" wrote:

> In 6, by default arguments, were passed byref what is the default in .NET?
> Can you point me to any MSDN documentation? TIA
>
>
>
Author
27 Jun 2006 5:01 AM
rmacias
That is incorrect.  When you pass a reference type ByVal, a pointer to a copy
of the data gets passed to the method.  You can manipulate the data as much
as you want with out affecting the value of the data from the calling method.

When you pass a reference type ByRef, you are actually passing the pointer
to that value to the method.  If you manipulate the value within the method,
the value is also affected in the calling method.

Public Sub Test()

    Dim x As Integer = 10

    PassingByVal(x)
    Console.WriteLine(x) ' value of x = 10

    PassingByRef(x)
    Console.WriteLine(x)  'value of x = 100

End Sub

Public Sub PassingByVal(ByVal x As integer)
    x = x * 2
End Sub

Public Sub PassingByRef(ByRef x as Integer)
    x = x * 2
End Sub

Show quoteHide quote
"Dennis" wrote:

> Be careful when passing reference type arguments like a Class By Value.  It's
> a little confusing but when these arguments are passed by Value, actually
> only a pointer to the argument will be passed and if you change any of the
> Class's properties in the sub/funciton, the change will be reflected in the
> calling program's instance of the class.
> --
> Dennis in Houston
>
>
> "prefersgolfing" wrote:
>
> > In 6, by default arguments, were passed byref what is the default in .NET?
> > Can you point me to any MSDN documentation? TIA
> >
> >
> >
Author
27 Jun 2006 7:49 AM
R. MacDonald
Hello, rmacias,

Actually, I think that Dennis has it right.

If passed ByVal the object passed cannot be replaced by a different
object in the routine being called.  However the called routine can
modify the properties of the object passed, and these changes will be
reflected in the object referenced in the calling routine.

If passed ByRef, the object itself can be replaced, so that when the
routine called returns, the reference passed might refer to an entirely
different object than that which was passed in.

Cheers,
Randy


rmacias wrote:

Show quoteHide quote
> That is incorrect.  When you pass a reference type ByVal, a pointer to a copy
> of the data gets passed to the method.  You can manipulate the data as much
> as you want with out affecting the value of the data from the calling method.
>
> When you pass a reference type ByRef, you are actually passing the pointer
> to that value to the method.  If you manipulate the value within the method,
> the value is also affected in the calling method.
>
> Public Sub Test()
>
>     Dim x As Integer = 10
>
>     PassingByVal(x)
>     Console.WriteLine(x) ' value of x = 10
>
>     PassingByRef(x)
>     Console.WriteLine(x)  'value of x = 100
>
> End Sub
>
> Public Sub PassingByVal(ByVal x As integer)
>     x = x * 2
> End Sub
>
> Public Sub PassingByRef(ByRef x as Integer)
>     x = x * 2
> End Sub
>
> "Dennis" wrote:
>
>
>>Be careful when passing reference type arguments like a Class By Value.  It's
>>a little confusing but when these arguments are passed by Value, actually
>>only a pointer to the argument will be passed and if you change any of the
>>Class's properties in the sub/funciton, the change will be reflected in the
>>calling program's instance of the class.
>>--
>>Dennis in Houston
>>
>>
>>"prefersgolfing" wrote:
>>
>>
>>>In 6, by default arguments, were passed byref what is the default in .NET?
>>>Can you point me to any MSDN documentation? TIA
>>>
>>>
>>>
Author
27 Jun 2006 7:50 AM
Cor Ligthert [MVP]
Rmacias,

In my idea incorrect.

ByVal sends the reference to the Reference by ref.types and the Value by
val.types (what is done by a copy of that)

ByRef uses in both situations a copy of the reference (what is done by a
copy of that).

ByVal does mean as well that when you are handling data in a refernce type
you are changing the actual object.

Although it is not nice coding to use this way as the byval can used in a
sub, it is nicer to use a function and return the object again, just for
readability.

Cor



Show quoteHide quote
"rmacias" <rmacias@newsgroup.nospam> schreef in bericht
news:3CCB835F-0CE2-4A07-932D-994B8033E0F8@microsoft.com...
> That is incorrect.  When you pass a reference type ByVal, a pointer to a
> copy
> of the data gets passed to the method.  You can manipulate the data as
> much
> as you want with out affecting the value of the data from the calling
> method.
>
> When you pass a reference type ByRef, you are actually passing the pointer
> to that value to the method.  If you manipulate the value within the
> method,
> the value is also affected in the calling method.
>
> Public Sub Test()
>
>    Dim x As Integer = 10
>
>    PassingByVal(x)
>    Console.WriteLine(x) ' value of x = 10
>
>    PassingByRef(x)
>    Console.WriteLine(x)  'value of x = 100
>
> End Sub
>
> Public Sub PassingByVal(ByVal x As integer)
>    x = x * 2
> End Sub
>
> Public Sub PassingByRef(ByRef x as Integer)
>    x = x * 2
> End Sub
>
> "Dennis" wrote:
>
>> Be careful when passing reference type arguments like a Class By Value.
>> It's
>> a little confusing but when these arguments are passed by Value, actually
>> only a pointer to the argument will be passed and if you change any of
>> the
>> Class's properties in the sub/funciton, the change will be reflected in
>> the
>> calling program's instance of the class.
>> --
>> Dennis in Houston
>>
>>
>> "prefersgolfing" wrote:
>>
>> > In 6, by default arguments, were passed byref what is the default in
>> > .NET?
>> > Can you point me to any MSDN documentation? TIA
>> >
>> >
>> >
Author
27 Jun 2006 9:03 AM
Larry Lard
rmacias wrote:
> That is incorrect.  When you pass a reference type ByVal, a pointer to a copy
> of the data gets passed to the method.  You can manipulate the data as much
> as you want with out affecting the value of the data from the calling method.
>
> When you pass a reference type ByRef, you are actually passing the pointer
> to that value to the method.  If you manipulate the value within the method,
> the value is also affected in the calling method.

What you have written is about *VALUE* types, and is completely not
true for reference types.

>
> Public Sub Test()
>
>     Dim x As Integer = 10

Integer is a *VALUE* type.

Show quoteHide quote
>
>     PassingByVal(x)
>     Console.WriteLine(x) ' value of x = 10
>
>     PassingByRef(x)
>     Console.WriteLine(x)  'value of x = 100
>
> End Sub
>
> Public Sub PassingByVal(ByVal x As integer)
>     x = x * 2
> End Sub
>
> Public Sub PassingByRef(ByRef x as Integer)
>     x = x * 2
> End Sub

--
Larry Lard
Replies to group please