-
August 2nd, 2012, 01:21 PM
#1
[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.
-
August 3rd, 2012, 01:07 PM
#2
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.
-
August 3rd, 2012, 03:46 PM
#3
Re: [Help] With <List>
Originally Posted by zips
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?
-
August 4th, 2012, 07:48 AM
#4
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.
-
August 4th, 2012, 11:25 AM
#5
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.
-
August 5th, 2012, 01:00 AM
#6
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!
-
August 5th, 2012, 01:02 PM
#7
Re: [Help] With <List>
Originally Posted by wutang001
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();
}
-
August 5th, 2012, 02:41 PM
#8
Re: [Help] With <List>
Originally Posted by thefollower
Like what TheGreatThulu explained you in his post; you can not re new 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|