Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Logical issue with a recursive function call

Logical issue with a recursive function call

Scheduled Pinned Locked Moved C#
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John ph
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • J John ph

      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

      D Offline
      D Offline
      dasblinkenlight
      wrote on last edited by
      #2

      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 with parentModel.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
      );
      }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups