Logical issue with a recursive function call
-
trying to figure out a way to get it right... 1. items in a collection fall in one of the category parent or orphan 2. items in a collection may have one or more independent parent / one or more orphan 3. Both (Parent and Orphan) may or maynot have Individual behaviour 4. If a parent has one or null behaviour it should be applied to children. Individual behaviour of children item be hidden 5. If a orphan has one or null behaviour it should be applied to it 6. An item will have only one or null behaviour trying to rephrase the following code...
private static void ApplyBehaviour(IEnumerator items, Behaviour behaviour = null)
{
while(items.MoveNext())
{
ItemModel item = (ItemModel)items.Current;if(item.type = parent) { parentModel parentModel = (parentModel)item; if(parentModel.HasBehaviour) { behaviour = parentModel.Behaviour; } ApplyBehaviour(parentModel.Children.GetEnumerator(), behaviour); if(parentModel.HasBehaviour) { behaviour = null; } } item.Behaviour = behaviour; } } }
inital call will be ApplyBehaviour(items); if two items with common parent and different behaviour, then the above code gets the first item and children right...takes the parent behaviour but the second item retains its behaviour and applie it to children. not sure whether i have explained in a way that someone can read and understand
- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
modified on Wednesday, June 1, 2011 8:37 AM
-
trying to figure out a way to get it right... 1. items in a collection fall in one of the category parent or orphan 2. items in a collection may have one or more independent parent / one or more orphan 3. Both (Parent and Orphan) may or maynot have Individual behaviour 4. If a parent has one or null behaviour it should be applied to children. Individual behaviour of children item be hidden 5. If a orphan has one or null behaviour it should be applied to it 6. An item will have only one or null behaviour trying to rephrase the following code...
private static void ApplyBehaviour(IEnumerator items, Behaviour behaviour = null)
{
while(items.MoveNext())
{
ItemModel item = (ItemModel)items.Current;if(item.type = parent) { parentModel parentModel = (parentModel)item; if(parentModel.HasBehaviour) { behaviour = parentModel.Behaviour; } ApplyBehaviour(parentModel.Children.GetEnumerator(), behaviour); if(parentModel.HasBehaviour) { behaviour = null; } } item.Behaviour = behaviour; } } }
inital call will be ApplyBehaviour(items); if two items with common parent and different behaviour, then the above code gets the first item and children right...takes the parent behaviour but the second item retains its behaviour and applie it to children. not sure whether i have explained in a way that someone can read and understand
- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
modified on Wednesday, June 1, 2011 8:37 AM
It looks like the problem is that once you finish the first sibling with custom behaviour, you reset the value of
behaviour
to null. In other words, the logic as coded starts ignoring the value passed in after discovering the first sibling withparentModel.HasBehaviour == true
. Changing the code as follows avoids this problem:if(item.type = parent) {
ParentModel parentModel = (parentModel)item;
ApplyBehaviour(
parentModel.Children.GetEnumerator()
, parentModel.HasBehaviour ? parentModel.Behaviour : behaviour
);
}