default in .NET byref or byval?

  • Thread starter Thread starter prefersgolfing
  • Start date Start date
P

prefersgolfing

In 6, by default arguments, were passed byref what is the default in .NET?
Can you point me to any MSDN documentation? TIA
 
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
 
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.
 
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
 
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,

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
 
rmacias said:
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.
 
Back
Top