"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