0

Very simple concept but I am striking out here. I am trying to build a simple file explorer like Path but I can't seem to get it straight.

The method to build the Path is:

private string BuildFullPath(List<Project> Children)
    {
        string path = string.Empty;
        foreach(Project project in Children)
        {
            if (this.ParentFolder == null)
            {
                path = this.Name;
            }
            else 
            {
                path += this.ParentFolder.Name + " > " + this.Name;
            }
        }
        return path;
    }

And the fake data is

Projects = new ObservableCollection<Project>();
        Project parentOne = new Project("Apple", true, null);
        Project parentTwo = new Project("Samsung", true, null);
        Project parentThree = new Project("Google", true, null);
        Project parentFour = new Project("Amazon", true, null);

        Project parentOneChildOne;
        parentOneChildOne = new Project("Mac", true, parentOne);
        Project parentOneChildTwo;
        parentOneChildTwo = new Project("iPhone", true, parentOne);
        Project parentOneChildThree;
        parentOneChildThree = new Project("iPad", true, parentOne);
        parentOne.Children.Add(parentOneChildOne);
        parentOneChildOne.Children.Add(new Project("MacBook", true, parentOneChildOne));
        parentOneChildOne.Children.Add(new Project("MacBook Pro", true, parentOneChildOne));
        parentOneChildOne.Children.Add(new Project("MacBook Air", true, parentOneChildOne));
        projects.Add(parentOne);

So the path for MacBook Pro should be Apple -> Mac- >MacBook -> MacBook Pro and the path for Mac should just be Apple -> Mac but can't seem to shake it.

2
  • What does your project class look like? I have a way better idea Commented Oct 5, 2017 at 18:42
  • 1
    in your Apple -> Mac- >MacBook -> MacBook Pro example I don't see you trying to add a - anywhere. Please show the exact string output you are trying to get and what you are actually getting. Commented Oct 5, 2017 at 18:44

1 Answer 1

2

Something along the line of this....

private string BuildFullPath(Project project)
    {
        string path = string.Empty;
        while(project != null) {
           if(path != string.Empty)
               path = "->" + path;
           path = project.Name + path
           project = project.ParentFolder;
        }
        return path;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.