Link to home
Start Free TrialLog in
Avatar of dsudbury
dsudbury

asked on

Generate a hierachy from a C# List

I design instruction, and do some programming. I am learning (arg) c# and lists to help me do my work quicker. I am sure creating hierarchical numbers from a list is computer science 101, so I will submit this and continue on with my struggle (pop, push peek??)

I am looking for a C# function (?) that I can pass my list to and get it back with the dirty work done.

Thanks in advance for any help you can provide.

David

=======================================================================
Here are the details:

I create a class type (?) to use as a list:
    class CCContent
    {
        public string HierarchNumber { get; set; }
        public string Alias { get; set; }
    }

I have this string content (tabbed to show hierarchy):
Chapter
      Section
            Topic
                  Page
            Topic
                  Page
      Section
            Topic
                  Page


I use my C# prowess to create this list:

0      HierarchNumber      
0      Alias            Chapter
1      HierarchNumber      
1      Alias            Section
2      HierarchNumber      
2      Alias            Topic
3      HierarchNumber      
3      Alias            Page
4      HierarchNumber      
4      Alias            Topic
5      HierarchNumber      
5      Alias            Page
6      HierarchNumber      
6      Alias            Section
7      HierarchNumber      
7      Alias            Topic
8      HierarchNumber      
8      Alias            Page

Now I want to create what turns out to be pesky hierarchical numbers:

0      HierarchNumber      1
0      Alias            Chapter
1      HierarchNumber      1.0
1      Alias            Section
2      HierarchNumber      1.0.1
2      Alias            Topic
3      HierarchNumber      1.0.1.1
3      Alias            Page
4      HierarchNumber      1.0.2
4      Alias            Topic
5      HierarchNumber      1.0.2.1
5      Alias            Page
6      HierarchNumber      1.1
6      Alias            Section
7      HierarchNumber      1.1.1
7      Alias            Topic
8      HierarchNumber      1.1.1.1
8      Alias            Page

Corresponding to this (tabbed to show hierarchy):

1 Chapter
      1.0 Section
            1.0.1 Topic
                  1.0.1.1 Page
            1.0.2 Topic
                  1.0.2.1 Page
      1.1 Section
            1.1.1 Topic
                  1.1.1.1 Page
Avatar of Ken Butters
Ken Butters
Flag of United States of America image

Not sure if this is what you are looking for.... but have you thought about storing the data in collections?

Define a Book as a collection of Chapters.
Define a Chapter as a collection of Sections.
Define a Section as a collection of Topics
Define a Topic as a collection of Pages.

Then each time you hit a chapter... you add it to the book current collection...
Each time you hit a section... you add it to the current Chapter collection etc.

Before developing the idea more... wanted to know if this sort of direction is what you were thinking.
Avatar of dsudbury
dsudbury

ASKER

Thanks for such a quick response. Gosh.

What I want to do is generate those numbers as a string and stick them back into each HierarchNumber spot in my list.

These numbers are destined to populate an xattribute as in 'li="1.1.1"'

David
ASKER CERTIFIED SOLUTION
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oooh thanks for this. I will check this out.

I wonder should I move the  
list[i].HierarchNumber = chap.ToString() + '.' +

Open in new window


into each else if?

So something like
list[i].HierarchNumber = chap.ToString()  

Open in new window

at the chapter level and so forth?

PS: Sections start at 0 because the folks who pay me insist on it :-)
Okay so start section at 0 and reset it to 0 as well.

No reason to move that line into each else if. That's redundant code. Just have it after the entire if block

You might want to have an else at the end to handle error cases
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow thank you all so much.
I awarded the bulk of the points to TommySzalapski  because the solution was substaintially what I wanted.
I awarded Buttersk the rest because I did implement TommySzalapski using switch.
Trust that is fair.

Again thanks for this almost instanteous response and solution!!!