Re: Is a form passed by value or reference?




"Ann Huxtable" wrote...

> Look forward to some answers.

You'll probably get many... ;-)

But to get the explanations in the right order, I have reordered your
question a bit...

> I though forms would be passed by reference. I certainly
> dont want copies of my form to be made before being passed
> as some of the forms are quite "heavy".

You never pass any *copies* of objects at all through the use of parameters.

The default is that you pass everything "by value", but what *is* actually
the value when you "pass a form"? The value you pass is that of the
argument, but the value isn't the actual instance, but a reference to the
instance...

>I wrote a method that accepted a form. The signature was as ff:
>
> void foo(ref Form frm) ;
>
> I had to remove the ref keyword to get it to compile.
> Any idea why?

You probably called it with something like this:

foo(ref this);

That isn't possible, as the "this" keyword is a *read-only* refernce to the
instance itself, i.e. that the reference isn't allowed to be changed, and as
such the *reference* isn't allowed to change.

But don't worry, just remove the keyword ref, and it will work as you want.

What the "ref" keyword actually means, is (in short) that you as argument
pass along a pointer to the variable you use as an argument, which can have
some bad side-effects.

Let's say you have this method:

void Change(ref Person p)
{
p = new Person("Kerry");
}

Now you create a Person, and "send it" to the method Change.

Person f = new Person("George");

Change(ref f);

After the call, f references the Person "Kerry".


Let's say you have this method:

void Change(ref Person p)
{
p = new Person("Kerry");
}

I.e. that with the "ref" keyword, you can change the value of the reference.

This is probably *not* what you want to do with the Form you "send" to the
foo-method.


Let's instead say you have this method:

void Change(Person p)
{
p.Name = "Kerry";
}

Create a Person, and "send it" to the method Change.

Person f = new Person("George");

Change(f);

p inside the method is a reference to the *same* instance as f. After the
call, f *still* references the same object as before, but it has changed the
value of a property.


Let's show a last example:

void Change(Person p)
{
p = new Person("Kerry");
}

Create a Person, and "send it" to the method Change.

Person f = new Person("George");

Change(f);

In this case p starts out with the same value as f, i.e. a reference to the
instance "George", but as p is a local variable inside the method, its value
is replaced by a new reference to a new instance. That (new) instance will
be eligable for garbage collection as soon as the method runs out of scope,
and nothing has happened to the instance f refers to, which it still does
afterwards.


HTH.

// Bjorn A


.



Relevant Pages

  • Re: Passing arguements by reference
    ... but doSomething doesnt change p unless i use "ref"? ... reference, hence both p and q pointing to the same object, not to different ... don't use the ref keyword. ... void Execute() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Passing Objects by ref keyword as a convention
    ... practice all objects should be passed by reference using the ref ... keyword generally speaking because as the writer of code you are ... conveying your intentions that an Object should/can be modified by your ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using the ref keyword.
    ... > Whenever I wanna change the parameter passed to a function I will use ... > the 'ref' keyword, that's fine, and it even makes sense when sending a ... > Reference Type, of course, isn't it being sent Byref as a default ... > the 'ref' keyword? ...
    (microsoft.public.dotnet.languages.csharp)
  • Using the ref keyword.
    ... Something is quite puzzled to me in using the 'ref' keyword, ... I'm sending a class object to the function, ... Reference Type, of course, isn't it being sent Byref as a default ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Pass by reference vs Pass by Value (is data aliasing a problem in C#?)
    ... You would pass a reference type with a ref keyword or out keyword when ... you need to modify what reference itself, not what the reference points to. ... // Passing an array to a method without the ref keyword. ...
    (microsoft.public.dotnet.languages.csharp)