Hi All,

This has been doing my head it and i expect its not as hard as i'm making it out to be.

I'm creating a WorkFlow Management app which is made up of Workflows, Task and Subtasks.

All i want to do loop through all Tasks in the Workflow. If a task in a Workflow has any child tasks, iterate them and do the same again ie, if child tasks exists, iterate, and so on.

Its really no different to a directory structure

Folder 1
Folder 1-a
Folder 1-b
Folder 1-b-i
Folder 1-b-ii
Folder 2

So what i'm looking to do is

Workflow: Drive to Shop
Task 1: Get in car
Sub task 1a: Get keys out of pocket
Task 2: Drive to Shop

My Tasks Table looks like this:

1 | Drive to Shop | None
2 | Task 1: Get in Car | 1
3 | Sub Task 1a: Keys | 2
4 | Task 2: Drive to shop | 1

How the bloody hell do i set up the for loops? Do i

1. create a method that takes the Task as a parameter
2. If child tasks exist, call the loop through all tasks
3. Call the same method again with the current task as the param?

hope this make sense


Have a google, its called recursion.

in your case the algorithm there is a very simple but time consuming loop,

if the structure is in order from 1,2,3....n..n+1 and the parent task always refers to a parent with parentID < currentTaskId you just just use a while and call a addChildTo(parentID) where you add the current task to the existing parentID.

if the parent does not exist then you should show some code of what you have develop until now so we can think a solution and tell you how to solve the problem, these are in general very simple problems and all they require is attention from the developer.

Anyway, this being a C# forum why don't you use a data structure ? something similar to the TreeNode where you have a class like this :

class Node 
public int ID { get;set;} 
public string Name { get;set;}
public int ParentID { get;set;}
public List<Node> Childs { get;set;}

public Node()
this.ID = this.ParentID = -1;
this.Name = "Default Node";
this.Childs = new List<Node>();

Or :

1. use XML file to save the structure then you should have no problem reading the node you want with all it's childs in one pass, it's a build in function in .Net .

2. if you don't like a XML solution use a memory structure to save the data , like a tree structure then I would use traversal methods. tree traversal are very simple algorithms.

so.. show us some code.