473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

default in .NET byref or byval?

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
7 9263
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
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
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Logico | last post by:
Hi everybody, I've tried to use the byref keyword for passing arguments to subroutines and functions in my ASP pages with VBScript, but it seems that both byref and byval are irrilevant, as simple...
2
by: pecker | last post by:
I have a com dll interface: Public Function Hulian_Acount_all(ByVal vIndex As Variant, _ ByRef vDate As Variant, _ ByRef vCount As Variant, _ ByRef vMoney As Variant, _ ByRef vMyDetail As...
6
by: Cc | last post by:
hi, is there a way to use byref on property set , because i would like to pass the value into the variable byref ?
4
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
8
by: Boni | last post by:
Dear all, I found out that I don' understand byVal/byRef in VB. There is a simple example: Why in the first test the result is 10,10 where in the second 0,20. Thanks for your help. Boni Module...
6
by: ari | last post by:
hey all, i have the following 2 classes: Public Class DataAccessLayer .... .... Public Sub GetRecords(ByRef ds As DataSet1) ds = New DataSet1
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.