Jump to content

Doubt with recurrence for C# program, please help!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
fhuneeus

fhuneeus

    Newbie

  • Members
  • Pip
  • 4 posts
Hi. I have a big doubt with recurrence, i'm dealing with a problem right here.

I'm doing a Mail Administrator program. In it you can create contacts, with a name, e-mail, telephone number, etc.
You can also create contact Groups, in which you can insert contacts. Also you can create a Group inside of another Group, and also you can add an existing Group inside of another Group.

For ex: I have 3 groups.
-Work
-Party
-Friends

If i go into the group Work, i can create another group inside of this group called TelefonicaSA.
Also, i can add the group Friends inside the group Work, Party and TelefonicaSA.

What i want to do is that when you delete the group Friends (of the main list obviously), that my method first.
1. Deletes Friends of the main list.
2. Searches for the groups inside Work and Party.
3. For ex, inside Work it will want to search for TelefonicaSA, and if it finds Friends inside TelefonicaSA, delete it.
4. For Party, it will look inside of it and find Friends instantly and delete it.

That is the thing. I would greatly appreciate help.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It really depends on how you plan to store the data. This doesn't even have to involve recurrence, depending on how you plan to organize the information. For example, you could store all your data in a database. Each group would have an ID number, a parent ID (top groups have parent 0), and a label. Then you could have another table that associates people with groups. A simple SQL query would delete all groups based on the label and a second could the associations.

Of course, if you're doing a different way of storing the data, none of the above applies :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
fhuneeus

fhuneeus

    Newbie

  • Members
  • Pip
  • 4 posts
Thank you for your reply. I think that would work. However, I don't know ANYTHING about databases, this is for a subject of my University Carreer and all we've passed is C# programming. Up to this moment i believe the only way to do what I want to do is recurrence, but I don't understand well how does recurrence work and how to apply it effectively for my case.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You have to decide on how you're going to store the data before you worry about how to delete it. For some structures, recurrence makes sense. For others it doesn't.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
fhuneeus

fhuneeus

    Newbie

  • Members
  • Pip
  • 4 posts
Yeah, I've got it decided. Each group is an object of type Group.
Group objects have an atribute which is List<Group> groupsinside. It's basically a list of Groups that is inside the same Group. When i want to create a Group inside, i do groupsinside.Add(Group a);

#6
hehewaffles

hehewaffles

    Newbie

  • Members
  • Pip
  • 9 posts
I'm a bit confused about what you're trying to accomplish.

You have a List of Group`s. Each Group can either contain another Group or some people? Maybe if you provided some actual definitions (code) of classes for us, we could better answer what you'd like? If you haven't quite worked that out, then a better definition of what you're trying to accomplish would benefit (at least) me in trying to help you.

#7
fhuneeus

fhuneeus

    Newbie

  • Members
  • Pip
  • 4 posts
Ok. I'll say more in respect to classes. My problem has changed however.

What i want to do now is to add for example all of the groups I have into a TreeView control. Also, when I add for example the group "Work", i also want to add inside that node other nodes that represent the groups that are inside the group Work. For this i need a method that looks for all of the groups, and if it has a subgroup add these subgroups inmediately. Also, once the subgroup is added, look for the subgroups of this subgroup.

I have a class called Contact. It has a boolean attribute called Isgroup. If this attribute is true, it is a group. If it is false, it is a contact. It also has an attribute called contactsgroup which is a List<Contact> where I store the contacts of this group and other subgroups created inside it.

In my class CCE (which is the class that represents each e-mail account, for ex mark.hotmail) i have an attribute called emailgroups. This attribute is a List<Contact> that containes all the groups that have been created. All of this have their attribute Isgroup in true.
In order to for ex create a group inside another group, I also have in my CCE class an attribute Contact currentgroup. This attribute represents the group which we are inside.

So for example, if the user wants to explore the group work, which is emailgroups[0], currentgroup = emailgroups[0] and then if he wants to create another group inside this one, he does currentgroup.contactsgroup.Add(new Contact);

Now, what i want to do is to search for each one of the groups, add it to a node, and then for each one of this grupos add it's subgroupds, and then the subgroups of this subgroups, etc.

Thanks.

#8
hehewaffles

hehewaffles

    Newbie

  • Members
  • Pip
  • 9 posts
Could I recommend a different way of doing it?

The best way, in my opinion to do this would be to use an interface IGroupMember, which has a string, name, and maybe a few other properties? Then you have two classes, Group and Contact, where a Group contains a List<IGroupMember>, and if you wanted to check if an IGroupMember was a Contact or Group, you would simply do

if (groupMembers[0] is Contact)
...

For adding things to your TreeNode, you could simply use a recursive method:

private void addToTree(TreeView treeView, IGroupMember GroupMember)
{
    if (GroupMember is Contact)
    {
        //I don't recall exactly how to use a TreeView, but you would add the contact here
        return;
    }

    //IGroupMember is assumed to be a group
    //add the node however into your TreeView
    foreach (IGroupMember groupMember in (GroupMember as Contact).Members)
    { //Members is assumed to be the List<IGroupMember> previously mentioned
         addToTree(treeView, groupMember);
    }
}

As for exploring groups, you could (if you're doing this yourself, and the TreeView doesn't support it, or you just want to do it yourself) likewise use a recursive method:

private List<IGroupMember> getGroupMembers(IGroupMember BaseGroup, string GroupName)
{
    if (BaseGroup is Contact)
        return null;
    if (BaseGroup.Name == GroupName)
        return (BaseGroup as Group).Members;
    List<IGroupMember> ret = null;
    foreach (IGroupMember groupMember in (BaseGroup as Group).Members)
    {
        ret = getGroupMembers(groupMember, GroupName);
        if (ret != null) //we found something
            break;
    }
    return ret; //this will be null if we didn't find anything, which is desired
    //otherwise, if we did find something, we return a populated List
}