Hi,
I have two classes A and B. I need to pass A to B so that when a graphic symbol in B is clicked it modifies code in A. i.e code in B needs to modify code in the passed class A.
public class B(ref A m_a){
}
Several classes( C and D) use class B and I have a class hierarchy such that C and D are derived classes of A.
I am having problems with conversion error messages when trying to pass references of derived classes. i.e
B b = new B(ref D) the compiler says that it cannot convert D to A
Is it possible to pass a reference object by reference when the object is a derivative (like above )
I am trying to pass by ref so that an object of B does not get loaded by the object D. I only want to pass the address and have B modify A.
If I pass an object of D without using ref , I realise that B will not be able to modify D (due to a copy of the reference D). Is it correct to say that if a reference to object D is passed without using ref, then (a copy) of the entire object D will be passed to B. Is this right
Thanks
Andrew.

Passing a reference argument using ref
Sam_res03
Thanks,
I will think about what you have said and will I read through the url article before asking any further questions.
Regards
Andrew.
xishan shigri
Related to your topic, I made a simple sample as following:
namespace
Work8{
public class Root{
private static string lang; static Root() { lang = "default"; } public string Language{
get { return lang; } set { lang = value; }}
public virtual void Show(){
System.
Console.WriteLine( "Root language {0}", this.Language );}
}
public class Sister{
public Root root; public Sister() { this.root = null; } public Sister(ref Root aRoot) { this.root = aRoot; }}
public class Child : Root{
public string Parent{
get { return base.Language; } set { base.Language = value; }}
public override void Show(){
System.
Console.WriteLine( "Parent language {0}",this.Parent);}
}
class Program{
static void Main(){
Root root = new Root();root.Show();
// "Root language default" Child child = new Child();child.Show();
// "Parent language default" Root x = child;child.Language =
"CSharp"; Sister sister = new Sister( ref x );sister.root.Show();
// "Parent language CSharp"sister.root.Language =
"Basic";root.Show();
// "Root language Basic"System.
Console.ReadKey();}
}
}
Now a have question related to class Sister constructor. How can I modify code so there is no explicit reference to class Root and a general variable to hold reference, not like now: public Root root;
It would be nice if class Sister constructor can get anonymous reference.
Predator14567
gshaf
You have to use a temp variable in that case
A a = D;
B b = new B(ref a);
No, only a copy of the reference. See http://www.yoda.arachsys.com/csharp/parameters.html
gokce
It's pretty odd to have an instance property (Root.Language) backed by a static field. It gives clients a rather unexpected behavior.
I'm not sure what you mean when you say you don't want to have an explicit reference to Root. But if you don't want to strongly type the parameter, you can replace it with an interface it implements (in this case Root doesn't implement any interface) or use System.Object as the type.
Finally, I don't see any reason for the parameter to the Sister contructor to be a ref parameter.