Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collections question

Status
Not open for further replies.

SmileeTiger

Programmer
Mar 13, 2000
200
US
What I am trying to do is create a sorted list of users using a collection in C#. I want them to be sorted based on userID. Below is what I have so far. Any ideas on what's wrong with my add function?


using System;
using System.Collections;
namespace ChatServer
{
class UserList
{
SortedList List;
private int NumberOfUsers;



public UserList() //Constructor
{
Console.WriteLine("Created new userlist");
List= new SortedList();
}

~UserList() //Destructor
{
Console.WriteLine("Deleting the user list");
}

public int Add(User NewUser)
{
return List.Add(NewUser.UserID, (NewUser) obj); //This is totally messed. I donno what I am doing here.
}
}


class User
{
public int UserID;
//public string UserName;

private string UserName;
public string Username
{
get
{
UserInfoAccessed++;
return UserName;
}
set
{
UserName=value;
UserInfoAccessed=0;
}
}

private bool OnlineStatus;
private int UserInfoAccessed;

public User(int UserID, string UserName, bool OnlineStatus) //Constructor
{
UserInfoAccessed=0; //The number of times the user info has been accessed.
this.UserID=UserID;
this.UserName=UserName;
this.OnlineStatus=OnlineStatus;
}

~User()
{
Console.WriteLine("Deleting user {0}", this.UserID);
}

public void GetUserInfo()
{
Console.WriteLine("\n***Begin User Record***");
Console.WriteLine("User: {0}", UserID);
Console.WriteLine("User Name: {0}", UserName);
Console.WriteLine("OnlineStatus: {0}", OnlineStatus);
Console.WriteLine("*** End User Record***\n");
}

public void Logout()
{
Console.WriteLine("User {0} has now been logged out...", UserID);
OnlineStatus=false;
}

}



class ChatServer
{
static void Main(string[] args)
{
User User1= new User(1, "Cory M Hicks", true);
User1.GetUserInfo();
User1.Username="Bob";
User1.GetUserInfo();
UserList UserList1= new UserList();
//UserList1.AddUserToList(User1);
UserList1.Add(User1);

}
}
}
 
1) Don't write destructors -- let the garbage collector do it's job. If you have a limited resource (like a graphics object or something), then implement the IDisposable interface.

2) There's no need to write a UserList class, as SortedList will do everything you need. Just instantiate one in your main() method.

3) It's good practice to prefix private variables with "m_" or possibly just an underscore, to indicate they're private.

4) I see you have a logout method. Is there some reason to keep track of users after they've logged out? If you want a list of all potential users (aka they're registered with your chat service, but not actually logged in at the moment), then store them in a database. The MSDE (desktop version of SQL Server 2000) is available for free on the MSDN site. Don't even think about using MS-Access, you'll stunt your career. ;-)

Chip H.
 
I eventually came up with this:

using System;
using System.Collections;
namespace ChatServer
{
class UserList
{
protected SortedList List;
private int NumberOfUsers;

public int NextIndex //Gets the next valid index number to use
{
get
{
return List.Count;

}
}

public User this[int index]
{
get
{
if(index > List.Count)
return (User)null;

return (User) List.GetByIndex(index);
}
set
{
if (index < 10)
List[index]=value;
}
}

public void Remove(int element)
{
List.RemoveAt(element);
}

public UserList() //Constructor
{
//Console.WriteLine(&quot;Created new userlist&quot;);
List= new SortedList();
}

~UserList() //Destructor
{
Console.WriteLine(&quot;Deleting the user list&quot;);
}


}


class User
{
public int UserID;
//public string UserName;

private string UserName;
public string Username
{
get
{
UserInfoAccessed++;
return UserName;
}
set
{
UserName=value;
UserInfoAccessed=0;
}
}

private bool OnlineStatus;
private int UserInfoAccessed;

public User(int UserID, string UserName, bool OnlineStatus) //Constructor
{
UserInfoAccessed=0; //The number of times the user info has been accessed.
this.UserID=UserID;
this.UserName=UserName;
this.OnlineStatus=OnlineStatus;
}

~User()
{
Console.WriteLine(&quot;Deleting user {0}&quot;, this.UserID);
}

public void GetUserInfo()
{
Console.WriteLine(&quot;\n***Begin User Record***&quot;);
Console.WriteLine(&quot;User: {0}&quot;, UserID);
Console.WriteLine(&quot;User Name: {0}&quot;, UserName);
Console.WriteLine(&quot;OnlineStatus: {0}&quot;, OnlineStatus);
Console.WriteLine(&quot;*** End User Record***\n&quot;);
}

public void Logout()
{
Console.WriteLine(&quot;User {0} has now been logged out...&quot;, UserID);
OnlineStatus=false;
}

}



class ChatServer
{
UserList UserList1= new UserList();
public static void Main(string[] args)
{
/* User User1= new User(1, &quot;Cory M Hicks&quot;, true);
User1.GetUserInfo();
User1.Username=&quot;Bob&quot;;
User1.GetUserInfo();
UserList UserList1= new UserList();
//UserList1.AddUserToList(User1);
// UserList1.Add(User1);
UserList1[UserList1.NextIndex] = User1;
UserList1[UserList1.NextIndex] = new User(1, &quot;a&quot;, true);

*/ ChatServer CS=new ChatServer();
CS.AddUser();
CS.ShowMenu();
}

private void AddUser()
{
string UserName;
Console.Write(&quot;Enter the UserName: &quot;);
UserName=Console.ReadLine();
Console.WriteLine(&quot;The name entered was {0}&quot;, UserName);
UserList1[UserList1.NextIndex] = new User(UserList1.NextIndex+1, UserName, true);
}
 
1) Don't write destructors -- let the garbage collector do it's job. If you have a limited resource (like a graphics object or something), then implement the IDisposable interface.

Fair enough.

2) There's no need to write a UserList class, as SortedList will do everything you need. Just instantiate one in your main() method.

I wrote one so I can sotre some other stuff in there as well as members of the list. Otherwise I would just use a SortedList.

3) It's good practice to prefix private variables with &quot;m_&quot; or possibly just an underscore, to indicate they're private.

I usually do this however (picked up the habit from doing VC++ coding) right now I am just learning and I am concentrating on learning the new features of the language.

4) I see you have a logout method. Is there some reason to keep track of users after they've logged out? If you want a list of all potential users (aka they're registered with your chat service, but not actually logged in at the moment), then store them in a database. The MSDE (desktop version of SQL Server 2000) is available for free on the MSDN site. Don't even think about using MS-Access, you'll stunt your career.


True enough. Right now I am focusing on SLOWLY expanding it. I'm not going to deal with databases at the moment. This is something that will get added latter.

I agree MS-Access for anything with alot of info is just hell :)



Thanks for the comments!
 
2(b) - why not have your UserList inherit from SortedList? That way you can extend it as needed, plus you don't have to rewrite all the usual boring collection management stuff.

5 - why are you making the NextIndex property public? If you had an .Add() method on your UserList, the info could stay private to the class (information hiding, and all that).

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top