blah blah blah is here! blah blah » Close

up0down
link

Let's say I have two lists; Users and Groups.

Users has the following fields: Name, Email, AccessLevel
Groups has the following fields: Name

However, Groups should also contain a list of users (a group is defined with multiple users). So, group "A" could be defined as User 1,2,6,9. These users must be users currently in the users list.

What is the best way to handle this? Can I somehow simply just create references to one of the existing user list items? Or should I create another list within groups which duplicates the information?

last answered 2 years ago

1 answers

link

Well, clearly your Group class needs to have a field of type List<User>.

However, Lists are reference types and so you can assign an existing List<User> object to that field and (as the following 'rough and ready' example shows), when you change that List, the change will automatically be reflected in the Group's List field because they are references to the same object:

using System;
using System.Collections.Generic;

enum AccessLevel
{
Ordinary,
Admin
}

class User
{
public string Name;
public string Email;
public AccessLevel AccessLevel;

public User(string name, string email, AccessLevel accessLevel)
{
Name = name;
Email = email;
AccessLevel = accessLevel;
}
}


class Group
{
public string Name;
public List<User> Users;

public Group(string name, List<User> users)
{
Name = name;
Users = users;
}

public void ListUserNames()
{
foreach(User u in Users)
{
Console.WriteLine(u.Name);
}
}
}

class Program
{
static void Main()
{
List<User> users = new List<User>();
User user1 = new User("Fred", "fred@acme.com", AccessLevel.Ordinary);
User user2 = new User("John", "john@acme.com", AccessLevel.Admin);
users.Add(user1);
users.Add(user2);
Group group1 = new Group("Accounts", users);
group1.ListUserNames();
User user3 = new User("Ann", "ann@acme.com", AccessLevel.Ordinary);
users.Add(user3);
Console.WriteLine();
group1.ListUserNames();
Console.ReadKey();
}
}

eeboy
499

That solves a portion of my problem. However, let's say that I have 10 users and I have 3 groups. These groups are made up of select users from my user list. So my groups look like so: Group1: Users 1,2,3,8 Group2: Users 3,7,10 Group3: Users 1,4,9,10 Now, let's say that I make a change to User 1's access level. I want that change to propagate to my user list in Group 1 and 3 (because User 1 is a member). Make sense? I really want to add a specific user of the list users and maintain the link.

vulpes
17279

As long as User is a class (not a struct), then any change you make to a User object will be propogated to the user lists of all Groups of which that User is a member. This is because the lists (and hence Groups) contain references to the same User object, not copies of it.

eeboy
499

Yes, and I see that works as in your example. However, the issue is that I can't seem to selectively add users to a group. I have to add the whole list or none at all. I would like to add users of the list to a group like so... Groups[0].Users = Users[3]; Groups[0].Users= Users[7];

vulpes
17279

To add users, you'll need to use syntax such as this: Groups[0].Users.Add(Users[3]); Groups[0].Users.Add(Users[7]);

eeboy
499

Exactly what I needed... thanks!

Feedback