L
Lakenarr
Hi,
I need to have two objects referencing eash other. Something like this:
class Planet
{
private Moon m_Moon;
public Moon Moon
{
get { return m_Moon; }
set { m_Moon = value; }
}
}
class Moon
{
private Planet m_Parent;
public Planet Parent
{
get { return m_Parent; }
set { m_Parent = value; }
}
}
So i can do something like this:
Planet p = new Planet();
Moon m = new Moon();
p.Moon = m;
m.Parent = p;
This looks good for this example but i might need to change these all the
time, think about Husband and Wife classes linking each other with lots of
divorce and marriages.
I could do something like this:
class Planet
{
private Moon m_Moon;
public Moon Moon
{
get { return m_Moon; }
set
{
if (m_Moon == value)
return;
m_Moon.Parent = null;
m_Moon = value;
if (m_Moon != null)
m_Moon.Parent = this;
}
}
}
So this would do the trick:
Planet p = new Planet();
Moon m = new Moon();
p.Moon = m;
But what if someone changes m.Parent, expecting it to assign that, i could
have an "internal" setter on Moon's setter of Parent but it doesn't solve my
problem in the assembly.
Is there any common way to do this? What would you suggest?
Thanks.
I need to have two objects referencing eash other. Something like this:
class Planet
{
private Moon m_Moon;
public Moon Moon
{
get { return m_Moon; }
set { m_Moon = value; }
}
}
class Moon
{
private Planet m_Parent;
public Planet Parent
{
get { return m_Parent; }
set { m_Parent = value; }
}
}
So i can do something like this:
Planet p = new Planet();
Moon m = new Moon();
p.Moon = m;
m.Parent = p;
This looks good for this example but i might need to change these all the
time, think about Husband and Wife classes linking each other with lots of
divorce and marriages.
I could do something like this:
class Planet
{
private Moon m_Moon;
public Moon Moon
{
get { return m_Moon; }
set
{
if (m_Moon == value)
return;
m_Moon.Parent = null;
m_Moon = value;
if (m_Moon != null)
m_Moon.Parent = this;
}
}
}
So this would do the trick:
Planet p = new Planet();
Moon m = new Moon();
p.Moon = m;
But what if someone changes m.Parent, expecting it to assign that, i could
have an "internal" setter on Moon's setter of Parent but it doesn't solve my
problem in the assembly.
Is there any common way to do this? What would you suggest?
Thanks.