C
cody
I have a OOP problem with the well known pattern where objects containing an
object which represents a list of subobjects. Now my problem is that the
ctor of a subobject indirectly calls the property in Foo which returns the
BarList, since to this time the ctor of BarList did not returned yet, the
reference "list" is still null and so the next call
to the property tries to initialize BarList again!
I know there are several ways to solve this problem but what is the
recommended way to avoid this wierd problem?
class Foo
{
public Bar FirstBar(){return BarList[0];}
BarList list;
public BarList BarList
{
get {if(list==null)list=new BarList();return list;}
}
}
class BarList
{
Bar[] bar;
public BarList(Foo foo)
{
// initialize bars
}
}
class Bar
{
Bar(Foo foo)
{
if (this==foo.FirstBar) // here recursive call initializing BarList
again and again.
{
// do someting
}
}
}
object which represents a list of subobjects. Now my problem is that the
ctor of a subobject indirectly calls the property in Foo which returns the
BarList, since to this time the ctor of BarList did not returned yet, the
reference "list" is still null and so the next call
to the property tries to initialize BarList again!
I know there are several ways to solve this problem but what is the
recommended way to avoid this wierd problem?
class Foo
{
public Bar FirstBar(){return BarList[0];}
BarList list;
public BarList BarList
{
get {if(list==null)list=new BarList();return list;}
}
}
class BarList
{
Bar[] bar;
public BarList(Foo foo)
{
// initialize bars
}
}
class Bar
{
Bar(Foo foo)
{
if (this==foo.FirstBar) // here recursive call initializing BarList
again and again.
{
// do someting
}
}
}