Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET
I have this class StudentsGroup that also contain HashSet<StudentsGroup>
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
//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
public void AddGroup(StudentsGroup group, params int[] ParentIndex)
{
    //method body
}
what I mean
compare these two case
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 15-Apr-13 12:56pm
Edited 15-Apr-13 14:10pm
v3
Comments
Sergey Alexandrovich Kryukov at 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 at 15-Apr-13 19:04pm
   
NOT trivial
Sergey Alexandrovich Kryukov at 15-Apr-13 19:11pm
   
Hm. It does not cancel my question, apparently...
—SA
ibrahim_ragab at 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 at 15-Apr-13 20:33pm
   
OK, and..?
—SA
Steve44 at 15-Apr-13 23:44pm
   
Consider re-designing your approach, e.g. using a Dictionary so you can resolve groups by name, etc. Then you can provide a list of names (i.e. the "path within the subgroups") to iteratively resolve the target group you want to identify and then operate on this group as needed. The list would allow you to address any number of levels as required.
ibrahim_ragab at 16-Apr-13 12:16pm
   
Thanks very much Steve44 ,solved

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);
}
  Permalink  
v2
Comments
Steve44 at 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)

  Print Answers RSS
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100