CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2012
    Posts
    27

    [Help] With <List>

    Hi
    to briefly explain what I'm trying to do, is the following:
    1 user creates a group, and the 2nd joins the specified group.

    My problem is that the Count is off, I'm doing something wrong obviously. Here is some code snippits
    So this is the actual function.
    Code:
    public class Groups
        {
            public User _User{ get; set; }
            public List<User> Users { get; set; }
    
           public Group(User user) {
    			_User = user;
                            Users = new List<User>();
    		}
    So this is where it gets used
    Code:
    Group group = new Group(user)
    case 1: //Create Group
    group.Users.Add(user);
    int eg = party.mUsers.Count();
    Console.WriteLine("Users" + eg);
                        break;
                    case 2: //Join Group
                        group.Users.Add(user);
                        int eg = party.mUsers.Count();
    Console.WriteLine("Users" + eg);
                        break;
    To explain a bit more, I have a Client - Server connection and there is two clients connected to the server. Case 1 is "user 1" (client 1) creating a group, and Case 2 is "user 2" (client 2) joining the group. Now, the problem is that the count is always 0. Why is this? how do I fix this?
    Thanks.
    Last edited by wutang001; August 2nd, 2012 at 01:34 PM.

  2. #2
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: [Help] With <List>

    Obviously you are showing incomplete code, but a quick guess would be that you are applying 'Group group = new Group(user)' each time, so that the count is always zero for a new instance.

  3. #3
    Join Date
    May 2012
    Posts
    27

    Re: [Help] With <List>

    Quote Originally Posted by zips View Post
    Obviously you are showing incomplete code, but a quick guess would be that you are applying 'Group group = new Group(user)' each time, so that the count is always zero for a new instance.
    Sorry to be a spoonfeeder but how do I prevent that from happening?

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: [Help] With <List>

    Where and how do you check the count, and what is party.mUsers? Your code is rather hard to make sense of, because not only you're not using standard naming conventions, you're not consistent about the one you are using (you have both "_" and "m" as prefixes, and camelCasing and PascalCasing for the same kids of members, etc...).
    If you're using party.mUsers.Count() to check the number of elements, then you're adding your users to one list (group.Users), and checking another (party.mUsers).
    Also, the line "Group group = new Group(user);" creates a local variable that goes out of scope when the method exits, so you might want to promote it to a member field of the class. But there are other considerations here: you need a way of checking if a group already exists or if a new one needs to be created, and a way of storing all the existing groups - you can use a List<Group>, or a Dictionary<User, Group> to do that.
    If you want more specific help, you're gonna have to show us more of your code, and describe in more detail what is it that you're trying to do.
    Last edited by TheGreatCthulhu; August 4th, 2012 at 07:52 AM.

  5. #5
    Join Date
    May 2012
    Posts
    27

    Re: [Help] With <List>

    Thanks for your help. The party.mUsers.Count(); was a type, it's supposed to be group.Users.Count();. I do take the other parts into consideration, like you said, checking to see if the user is already in a group, but for now I want to just get this Group.Users.Count(); working.

    So like you said, I should promote it into another field in the class. Did you mean something like this?
    Code:
    public void CountMembers()
    {
    Users = new List<User>();
    int count = Users.Count();
    }
    I will post more code once I get home.

  6. #6
    Join Date
    May 2012
    Posts
    27

    Re: [Help] With <List>

    Ok, I will clear everything up now.

    I have a TCP server that allows clients to connect and chat. I want a user (Specified in the User.cs Class) to create a group, then be able to invite other connected clients to the group. Once the creator (or leader) of the group has invited another connected user, the connected can accept the invitation and be added to the group. Once the connected client (user) has accepted the invitation, the server console will display the group size. (amount of users in group)

    It's quite simple, I relize, but as I am a noob at C# I would appreciate some help.

    Thanks to everyone who has helped me so far!

  7. #7
    Join Date
    Apr 2012
    Posts
    29

    Re: [Help] With <List>

    Quote Originally Posted by wutang001 View Post
    Thanks for your help. The party.mUsers.Count(); was a type, it's supposed to be group.Users.Count();. I do take the other parts into consideration, like you said, checking to see if the user is already in a group, but for now I want to just get this Group.Users.Count(); working.

    So like you said, I should promote it into another field in the class. Did you mean something like this?
    Code:
    public void CountMembers()
    {
    Users = new List<User>();
    int count = Users.Count();
    }
    I will post more code once I get home.
    Like what TheGreatThulu explained you in his post; you can not renew your object Users each time the CountMembers is called like that. It may look something similarly as
    Code:
    public int CountMembers()
    {
         return Users.Count();
    }

  8. #8
    Join Date
    May 2012
    Posts
    27

    Talking Re: [Help] With <List>

    Quote Originally Posted by thefollower View Post
    Like what TheGreatThulu explained you in his post; you can not renew your object Users each time the CountMembers is called like that. It may look something similarly as
    Code:
    public int CountMembers()
    {
         return Users.Count();
    }
    Thanks! Do I still declare Users as a new list in the Group function?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured