Reputation: 3989
I know I have done this before but I am getting my constructor order of execution in a twist I think....
public class Class1
{
Class2 _class2;
public Class1()
{
_class2 = new Class2(this);
}
}
public class Class2
{
Class1 _parent; //corrected typo
public Class2(Class1 parent)
{
_parent = parent;
}
}
trouble is that parent always ends up null.
What's the proper way to do this? (maybe I can blame my slowness on having a cold..)
EDITED TO CORRECT THE TYPO (which isn't the problem in the real code!)
Upvotes: 10
Views: 8404
Reputation: 1816
I know this is an old question, but I thought I'd throw my two cents in for good measure. Google brought me here so it may bring someone else here.
This suspiciously looks like a circular dependency... it's quite the code smell to have two independent classes that use/reference each other.
If you want to have a parent/child relationship for objects, consider making a binary tree implementation (or similar) for your class.
If you want to exploit class inheritance, use it correctly >
Class2
is a base class for Class1
and your Class1
declaration would be public class Class1 : Class2
. You can refer to Class2
's methods in Class1
with the base
keyword.
So in short, this answer is: you need to redesign your classes to better fit what you're trying to accomplish, rather than fight the compiler to do something that's more confusing than it needs to be.
Especially since the code you're going to be testing this with is probably newing up Class1
, then querying that object for its reference to Class2
's reference for Class1
... which is absolutely pointless.
Append2:
Semantically, Class1
can be rewritten as follows:
public class Class1
{
Class2 _class2;
public Class1()
:this(new Class2(this))
{
}
public Class1(Class2 class2)
{
_class2 = class2;
}
}
... except it can't, because :this(new Class2(this))
is rightfully incorrect syntax. It's also exactly what you're doing. Please don't.
Upvotes: 1
Reputation: 18472
I don't see why this shouldn't work. It works with me.
declared: http://vvcap.net/db/I2OZoapbIRREvQ8ymPym.htp
stepped over: http://vvcap.net/db/ehsYqCY6JByqZQq-RXGp.htp
here's the result: http://vvcap.net/db/ZWjqb_Yv1yAisX0BYUns.htp
Upvotes: 0
Reputation: 564383
This should, technically, work, provided you change Class2 to include this.parent = parent;
However, I don't recommend this. I would, instead, recommend lazy initializing your class2 instance inside class1. Depending on what all is done in the constructor of Class2, you can potentially lead yourself into nasty situations.
Making a Class2 property on class1 and lazy initializing it would cause Class2 to be constructed after Class1's constructor is completed, not during it's construction, which is most likely less error prone if your classes get more complicated.
Upvotes: 10
Reputation: 26426
You may have mistyped the code, but I think you want this definition for Class2 (notice the this qualifier in your Class2 constructor):
public class Class2
{
Class1 parent;
public Class2(Class1 parent)
{
this.parent = parent;
}
}
Upvotes: 10
Reputation: 41813
Class1 parent;
_parent = parent;
_parent is never defined; you misspelled it.
Upvotes: 2