RE: Passing arguements by reference



You never pass actual objects, but references to those objects.

When you pass a reference to an object by 'ref', you are passing the actual
reference to the object - this reference can be changed by the method upon
return.

When you pass a reference to an object by value, you are passing a copy of
the reference to the object - this reference cannot be changed by the method
upon return.

For both cases, the method can manipulate and change the internals of the
object, since a reference and a copy of a reference point to the same object.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter



"Andrew Bullock" wrote:

Hi,

I have two classes, A and B,

B takes an A as an argument in its constructor:


A a1 = new A();
B b = new B(a1);

///

A a2;
public B(A aa)
{
a2 = aa;
}




However, I figured that I could reduce memory usage and increase
performance by passing aa by reference.

How do I alter my code to do this?

I know I need to do this:

public B(ref A aa)

but how do i declare a2? If i leave it as it is, does that mean a1 and
a2 both point to the same memory location, or when assigning a2 is the
object copied?


Thanks

Andrew

.



Relevant Pages

  • Re: pointer syntax
    ... > really passing a reference (which is like an old-school pointer, ... > a copy of the primitive's value and passing that. ... build them yourself with pointers. ... mean a var parameter, write a var parameter. ...
    (comp.lang.pascal.delphi.misc)
  • Which is better, pass, or not pass by reference?
    ... passing by reference. ... My understanding of passing by reference (putting the & before a variable ... the memory usage for this data... ...
    (comp.lang.php)
  • Re: Passing by reference
    ... It depends upon the reason one is passing the reference. ... instantiate all relationships via a constructor so there will always be ... I naturally think of 'getter' methods as 'knowledge ...
    (comp.object)
  • Re: Using ref
    ... the address of a variable that contains a reference to an object, ... the C version is passing the address of a variable that contains the   ... Inasmuch as the specification is unambiguous, there is a single truth. ... Every time he states the incorrect view, I will feel compelled to correct his incorrect statements. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Derived type argument to subroutine
    ... In practice, they often, but not always are. ... I can rely on the fact that arguments are passed by reference. ... Would the printed value always be 4, or is it undefined as the passing ... With an important exception, ...
    (comp.lang.fortran)