Jump to content
Xtreme .Net Talk

Recommended Posts

Posted


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.

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • *Experts*
Posted

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

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...