Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this class StudentsGroup that also contain HashSet<StudentsGroup>
C#
public class StudentsGroup
{
    public string Name;
    public int StudentsCount;
    public HashSet<StudentsGroup> SubGroups;

    public StudentsGroup()
    {
        SubGroups = new HashSet<StudentsGroup>();
        Name = "no_name";
    }
    public StudentsGroup(string name,int studens_count=0):this()
    {
        Name = name;
        StudentsCount = studens_count;
    }
}

how can I add element to SubGroups and its SubGroups in hierarchical way.
for example i can add item using this code
C#
//ex AddGroup(new StudentsGroup("first year, section one,group 1,subgroup 1"),0,0,0);
            this.SubGroups.
                ElementAt(0).SubGroups.
                ElementAt(0).SubGroups.
                ElementAt(0).SubGroups.
                Add(new StudentsGroup("first year , section one , group 1 , subgroup 1"));

Now how can I implement function AddGroup that take element instance and parent index,and if parent not exist return error
C#
public void AddGroup(StudentsGroup group, params int[] ParentIndex)
{
    //method body
}

what I mean
compare these two case
C#
StudentsGroup school = new StudentsGroup("school");
//add three years each year contain two classes
school.SubGroups.Add(
	new StudentsGroup("year1"));
 school.SubGroups.Add(
	new StudentsGroup("year2"));
 school.SubGroups.Add(
	new StudentsGroup("year3"));
school.SubGroups.ElementAt(0).SubGroups.Add(
	new StudentsGroup("class1"));
school.SubGroups.ElementAt(0).SubGroups.Add(
	new StudentsGroup("class2"));
school.SubGroups.ElementAt(1).SubGroups.Add(
	new StudentsGroup("class1"));
school.SubGroups.ElementAt(1).SubGroups.Add(
	new StudentsGroup("class2"));
school.SubGroups.ElementAt(2).SubGroups.Add(
	new StudentsGroup("class1"));
school.SubGroups.ElementAt(2).SubGroups.Add(
	new StudentsGroup("class2"));
//if i implement AddGroup(group,parent index)
school.AddGroup(new StudentsGroup("year1"), 0);
school.AddGroup(new StudentsGroup("year2"), 0);
school.AddGroup(new StudentsGroup("year3"), 0);
school.AddGroup(new StudentsGroup("class1"), 0,0);
school.AddGroup(new StudentsGroup("class2"), 0, 0);
school.AddGroup(new StudentsGroup("class1"), 0, 1);
school.AddGroup(new StudentsGroup("class2"), 0, 1);
school.AddGroup(new StudentsGroup("class1"), 0, 2);
school.AddGroup(new StudentsGroup("class2"), 0, 2);

//and how about when we have 10 level of subGroups , dynamic add using method will facilitate code writing
Posted
Updated 15-Apr-13 13:10pm
v3
Comments
Sergey Alexandrovich Kryukov 15-Apr-13 18:11pm    
Looks trivial. So, what's the problem? What did you try? If you try something, why your attempt was unsatisfactory?
—SA
ibrahim_ragab 15-Apr-13 19:04pm    
NOT trivial
Sergey Alexandrovich Kryukov 15-Apr-13 19:11pm    
Hm. It does not cancel my question, apparently...
—SA
ibrahim_ragab 15-Apr-13 19:22pm    
//when we have 10 level subgroups
school.SubGroups
.ElementAt(0).SubGroups
.ElementAt(0).SubGroups
.ElementAt(0).SubGroups
.ElementAt(0).SubGroups
.ElementAt(0).SubGroups
.ElementAt(0).SubGroups
.ElementAt(0).SubGroups
.ElementAt(0).SubGroups
.ElementAt(0).SubGroups
.ElementAt(0).SubGroups
.Add(new StudentsGroup("class2"));
//if i implement function AddGroup
school.AddGroup(new StudentsGroup("class2"),0,0,0,0,0,0,0,0,0,0,0);
Sergey Alexandrovich Kryukov 15-Apr-13 20:33pm    
OK, and..?
—SA

1 solution

C#
public void AddGroup(StudentsGroup group, params int[] ParentIndex)
{
	HashSet<StudentsGroup> last_subGroups = this.SubGroups;
	StudentsGroup current = this;
	for (int i = 1; i < ParentIndex.Length; i++)
	{
		last_subGroups = current.SubGroups;
		current = current.SubGroups.ElementAt(ParentIndex[i]);
	}
	current.SubGroups.Add(group);
}
 
Share this answer
 
v2
Comments
Steve44 16-Apr-13 13:48pm    
Yep, that was the traversal as proposed. However I still think the indexing of a HashSet by ElementAt is resulting in a pretty brittle decision as it can change when adding new sub-groups in the hierarchy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900