Diesel Posted September 2, 2005 Posted September 2, 2005 public class Class1 { private int member; public int Member { get { return this.member; } set { this.member =value; } } }//end class public class Class2 { private int externalClassMember; public int ExternalClassMember { get { return this.externalClassMember; } set { this.externalClassMember = value; } } public void SetMember(ref int member) { this.externalClassMember = member; } } //end class public class Test { Class1 testClass1 = new Class1(); Class2 testClass2 = new Class2(); testClass1.Member = 10; testClass2.SetMember(ref testClass1.Member); testClass2.ExternalClassMember = 8; Console.WriteLine(testClass1.Member.ToString()); } [/Code] I want the property in class2 to point to the property in class1. Problem is, I get an error that properties cannot be passed as a ref parameter. Quote
Administrators PlausiblyDamp Posted September 3, 2005 Administrators Posted September 3, 2005 A property is really designed to encapsulate or mimic a simple member variable of the class or structure; part of this means there should be no unexpected side effects when assigning or retrieving a value via a property - passing by reference would break this rule. It may help if you gave a bit more detail about what you are trying to do and see if anyone has an alternate suggestion. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* Nerseus Posted September 3, 2005 *Experts* Posted September 3, 2005 If I get you right, you want Class2 to hold a reference to an instance of Class1 - maybe pass that in the constructor. Then your property in Class2 just returns the value from Class1. Let me know if that doesn't make sense and I can throw out a sample - or maybe that's not even what you want. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Diesel Posted September 5, 2005 Author Posted September 5, 2005 Ok, I've decided that what Im doing is bad design anyway. I'll just stick with what I have. Thank you for your time. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.