• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 586
  • Last Modified:

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
0
dsudbury
Asked:
dsudbury
  • 3
  • 2
  • 2
2 Solutions
 
Ken ButtersCommented:
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.
0
 
dsudburyAuthor Commented:
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
0
 
TommySzalapskiCommented:
Why does section start at 0 and the rest start at 1?
Just as you go through the list keep four ints. Start them all at 1 or 0 then if you reach a section, increment the section number and reset topic and page.

int chap = 1;
int sec = 1;
int topic = 1;
int page = q;
for (int i = 0; i < list_size; i++) //or a list iterator depending on what type of 'list' you are using
{
  if (list[i].Alias == "Chapter")
  {
    chap++;
    sec = topic = page = 1;
  }
  else if(list[i].Alias == "Section")
  {
    sec++;
    topic = page = 1;
  }
  //etc

  list[i].HierarchNumber = chap.ToString() + '.' + ...etc
}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dsudburyAuthor Commented:
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 :-)
0
 
TommySzalapskiCommented:
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
0
 
Ken ButtersCommented:
This will produces the following output...

1
1.0
1.0.0
1.0.0.0
1.1
1.1.0

if you want to save the strings somewhere...instead of printing them... that would still use the same logic:



{
    class Program
    {
        static void Main(string[] args)
        {
            CCContent contentList = new CCContent();
            CurrentSectionCount CSCount = new CurrentSectionCount();
            
            Console.WriteLine(CSCount.getHierarchyNumber("Chapter"));
            Console.WriteLine(CSCount.getHierarchyNumber("Section"));
            Console.WriteLine(CSCount.getHierarchyNumber("Topic"));
            Console.WriteLine(CSCount.getHierarchyNumber("Page"));
            Console.WriteLine(CSCount.getHierarchyNumber("Section"));
            Console.WriteLine(CSCount.getHierarchyNumber("Topic"));
             
        }
    }
    class CCContent
    {
        public string Alias { get; set; }

    }
    class CurrentSectionCount
    {
        public int Chapter { get; set; }
        public int Section { get; set; }
        public int Topic { get; set; }
        public int Page { get; set; }

        public String getHierarchyNumber(string Alias)
        {
            string returnText = "";
            switch (Alias)
            {
                case "Chapter":
                    Chapter += 1;
                    Section = -1;
                    Topic = -1;
                    Page = -1;
                    returnText = Chapter.ToString();
                    break;
                case "Section":
                    Section += 1;
                    Topic = -1;
                    Page = -1;
                    returnText = Chapter.ToString() + "." + Section.ToString();
                    break;
                case "Topic":
                    Topic += 1;
                    Page = -1;
                    returnText = Chapter.ToString() + "." + Section.ToString() + "." + Topic.ToString();
                    break;
                case "Page":
                    Page +=1;
                    returnText = Chapter.ToString() + "." + Section.ToString() + "." + Topic.ToString() + "." + Page.ToString();
                    break;
            }

            return (returnText);

        }
    } 
}

Open in new window

0
 
dsudburyAuthor Commented:
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!!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now