By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,871 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,871 IT Pros & Developers. It's quick & easy.

default in .NET byref or byval?

P: n/a
In 6, by default arguments, were passed byref what is the default in .NET?
Can you point me to any MSDN documentation? TIA
Jun 26 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

"prefersgolfing" <pr*************@hotmail.com> wrote in message
news:eB*************@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

Jun 26 '06 #2

P: n/a
Default is ByVal. See the Remarks (Rules) section here:
http://msdn2.microsoft.com/en-us/library/cbs7z96t.aspx

/claes

"prefersgolfing" <pr*************@hotmail.com> wrote in message
news:eB*************@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

Jun 26 '06 #3

P: n/a
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

Jun 26 '06 #4

P: n/a
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

Jun 27 '06 #5

P: n/a
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:
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

Jun 27 '06 #6

P: n/a
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" <rm*****@newsgroup.nospam> schreef in bericht
news:3C**********************************@microsof t.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
>
>
>

Jun 27 '06 #7

P: n/a

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.

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

Jun 27 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.