Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

ByRef usage?

Hi,

Are there any documents or articles talking about .NET discouraging the use of ByRef?

If I have a custom class, should not I pass the instance of that class to a method as ByRef parameter value?
Avatar of kaufmed
kaufmed
Flag of United States of America image

It depends on your system and requirements. Passing by reference is often better in terms of performance because you are not passing the entire set of data to the method; rather you only passing a reference. In this way, both the caller and the callee see the same object in memory. This behavior can cause confusion for programmers who don't fully understand working with references as well as make it difficult to code parallel code.
SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I notice that you updated your comment prior to my post. My comment is in reference to the original comment you had  : )
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, ByVal and ByRef, are relevant to 'value types' like int, char, float and are basically overkill and irrelevant if used with 'reference types' like StringBuilder, SqlConnection, etc

Reason?

With reference types, when you pass them around in methods, they are already passed 'ByRef'. meaning that the called method, can actually effect changes to the member attributes or properties inside the ref types passed to it as parameters.

However, if you don't want the method called to impact any of the member attributes or properties of the passed ref type parameter, pass them using 'ByVal'

Similarly, 'ByVal' has no significance when value types are passed around as parameters to methods since by default, they are passed as 'ByVal'

But if you want, the called method to effect changes or updates to the value type parameters being passed, you should use 'ByRef'

I don't thing there is performance penalty in either of the case.

Does it help.

You can refer to MSDN on this link

http://msdn.microsoft.com/en-us/library/ddck1z30(v=vs.71).aspx
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
After some further thought on the matter, and after reading some of the recent comments, I think I focused more on the concepts of passing by value and passing by reference rather than the actual keywords ByVal and ByRef as they pertain to .NET. For that I will rescind my first comment as it can be cause for confusion.
A lot of discussion for a very simple question.

If I have a custom class, should not I pass the instance of that class to a method as ByRef parameter value?

If what you want to do is to use a class instance and/or modify it properties, it makes no difference whether you pass it ByVal or ByRef.

The convention and usually the best way to do that is ByVal because of the boxing and unboxing that can happen when passing some types of object ByRef, although it does not come into play when the object is an instance of a class.
@JamesBurger

Please cite a source for the following assertion:

...because of the boxing and unboxing that can happen when passing some types of object ByRef...
I learned of the mechanism at a Microsoft DevDays a few years ago, and their demonstration then was very clear.

I am however unable to find something very definitive. A search on "boxing unboxing" turns on a lot of hits, but at first sight, none is complete. You have to put things together from many references.

Most of the interesting pages deal with C# because C# programmers often have a background in C and understand the concepts of memory management through the stack an the heap. These are involved in the operation because value objects (structures) are stored on the stack while reference objects (classes) are handled in the heap).

This one for VB (http://msmvps.com/blogs/joacim/archive/2009/08/31/boxing-and-unboxing-in-net.aspx) does the same thing as almost all the other ones, it deals with ways to box and unbox yourself and does not associate the feature with ByRef.

This one from MSDN(http://msdn.microsoft.com/en-us/library/cc586700.aspx) indicates that boxing is implicitely used when passing parameters by reference.

And since the thing is a CLR feature, second sentence from (http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx) indicates that it happens in all the .NET languages. This was very clear at the DevDays. Even COBOL.NET has it.
The reason I ask is because of the 4th sentence of the Note here:  http://msdn.microsoft.com/en-us/library/14akc2c7.aspx

There is no boxing of a value type when it is passed by reference.

Admittedly, a C# link, but my understanding is that the ref keyword is the equivalent of using ByRef in VB.NET.
Avatar of dkim18
dkim18

ASKER

It is a little confusing.

JamesBurger is saying it doesn't matter which to use:

If what you want to do is to use a class instance and/or modify it properties, it makes no difference whether you pass it ByVal or ByRef.

If you pass a reference object (based on a class), the difference between ByVal and ByRef is minimal. With ByVal, you are not passing a copy of the object, but only a copy of the pointer.

I didn't really think of this in C# an Java.

It seems most of you are saying that I need to use the ByRef if I want to modify object properties.
Is that right?
It seems most of you are saying that I need to use the ByRef if I want to modify object properties.
Is that right?
No, not quite. If you want to modify the properties only, then it does not matter if you use ByRef or ByVa. If you want to modify the properties and the variable itself, then use ByRef. (See my "New" example or Medo3337's example above to see what we mean when we say "modify the variable".)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkim18

ASKER

I apologize in advance if I am not understanding you in first place.


can you explain again what you mean properties and variable?
Are you talking about Property and Field variables?
Variables are those things you declare with Dim:

Dim x As Integer
x=10

Properties are those values you can call on objects, that are shown by a little hand holding a piece of paper in the lists that open automatically on the screen in Visual Studio 2010 and less, and that you call with the following syntax:

myTextBox.Text = "foo"
myLabel.Width = 100

Fields are something you do not encounter often. They are in someway a combination variable/property. You use them as if they were properties, but in fact they are variables declared in the class or structue that define the object. In the IntelliSense lists of Visual Studio 2010 and less, they are shown with little blue boxes.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial