Frage

I am creating an applicaiton that requires the use of recursive aggregation in one of the classes. The class at hand is entitiled "component" and a component may be made up of sub components stored within a list. The following code shows this.

    public class Component {
//vars for class
public String componentName;
public String componentShape;
public String componentColour;
public String componentMaterial;
public int numChildComps;

//list to store child components of component
public List<Component> childComponents;

public Component(string _componentName, string _componentShape, string _componentColour, string _componentMaterial, int _numChildComps)
{
    componentName = _componentName;
    componentShape = _componentShape;
    componentColour = _componentColour;
    componentMaterial = _componentMaterial;
    numChildComps = _numChildComps;

    //if component has no child components set list to null
    if (numChildComps == 0)
    {
        //instatiate new list
        childComponents = new List<Component>();
        childComponents = null;
    }
    else if(numChildComps != 0)//if not null then create child components for the amount stated above.
    {
        childComponents = new List<Component>();
        for (int i = 0; i < numChildComps; i++)
        {
            Console.WriteLine("Add details for child component " + (i+1));
            Console.WriteLine("Enter component Name: ");
            string name = Console.ReadLine();
            Console.WriteLine("Enter shape: ");
            string shape = Console.ReadLine();
            Console.WriteLine("Enter Colour: ");
            string colour = Console.ReadLine();
            Console.WriteLine("Enter Material: ");
            string material = Console.ReadLine();
            Console.WriteLine("Enter num child components: ");
            string num = Console.ReadLine();
            childComponents.Add(new Component(name, shape, colour, material, Int16.Parse(num)));//instatiate new child component with params and add to the list.
        }
    }
}

This will instaiated a class and if the parameter for number fo child components is more than 0 then it will create the object and store it in the list "childComponents". This works fine. My question is how would I go about retrieving the items within the list. Take the following as an example, I have a model which is made up of one component but that component has 2 components and one of those has another 2 and one of those has one component:

Model
 -component
  -childComponent
    -childComponent
    -childComponent
      -childComponent
  -childComponent

Obviously this could go on forever and I have tried creating a piece of code to retireve all the components and sub components but it has not worked as you need to know the total amount of components a model has and then that components childComponents and so on.

Code that I have tried(does not model the above example)

    IEnumerable<SEModel> modelres = from SEModel sm in database
                                    select sm;
    foreach (SEModel item in modelres)
    {
      Console.WriteLine(item.getSetModelName);
      Console.WriteLine(item.componentList.First().componentName);
      foreach (SEComponent citem in item.componentList)
      {
       Console.WriteLine(citem.childComponents.First().componentName);
        foreach (SEComponent scitem in citem.childComponents)
        {
          Console.WriteLine(scitem.componentName);
        }
     }

Like stated above you would have to know the amount of components with childcomponents and then their childcomponents and so on.

War es hilfreich?

Lösung

public IEnumerable<Component> GetComponents()
{
    yield return this;

    foreach( var childComp in childComponents )
    {
        foreach( var comp in childComp.GetComponents() )
        {
            yield return comp;
        }
    }
}

If you're not sure what yield return is all about, read this

Andere Tipps

If you need to retrieve the components as a flat list you can do it this way:

public List<Component> GetAllComponents(List<Component> components)
{
   var result = new List<Component>();
   result.AddRange(components);

   foreach (var component in  components)
        result.AddRange(GetAllComponents(component.childComponents));

   return result;
}

var allComponents = GetAllComponents(Model.Components);

Though I am not sure from your question what exactly you want to do.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top