By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,156 Members | 1,069 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,156 IT Pros & Developers. It's quick & easy.

wierd OOP problem (a bit complicated)

P: n/a
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
}
}
}

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
cody wrote:
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
}
}
}


All I can make of it is that you're trying to define a list of type T and
you want to pass the instance of T which contains an object O to that same
object O when O is constructed. So far this will not be a problem, however in
O's constructor you're trying to check if O is the first instance in the list
O is in. This dives into recursion in your solution because you're doing test
actions in the constructor, while the constructor should just be a routine to
setup the object with default values and initialize it a bit (not much).

However if you change Foo.FirstBar into:
public Bar FirstBar
{
get
{
if(list==null)
{
return null;
}
else
{
return list[0];
}
}
}

it should work, however perhaps I don't understand what you're trying to
achieve. I think your code can be restructured for what you want to achieve.

FB

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.