Sign in to follow this  
Iderik

[SOLVED] [C#] Store connections

Recommended Posts

Hi, I've been trying to ask for help in another thread, but I think it was so confusing so people didn't understand what I was saying. When a client connects, I want to create a new Player object which contains IP, name, x and y. Then for example I want to update or send a message to a player, I want to search/look up/access with IP or name. Here is a way I thought about doing, but doesnt really work. And I'm pretty sure this isnt the way of doing it. Maybe a dictionary would work, but how?
[SOURCE]
List<Player> lPlayerList = new List<Player>();

string myString = "127.0.0.1";

// Here I want to create a player object with the name of string playerName1, but it doesnt work.
Player myString = new Player();
[/SOURCE]
[/source] Could anyway PLEASE explain so a beginner understands, I would be really thankful for help, because I've been stuck here for ages. [Edited by - Iderik on October 9, 2008 7:38:15 PM]

Share this post


Link to post
Share on other sites
In that code sample, you are trying to create two objects, both of which have the same name "myString". Objects have to have unique names. You can't create a Player called myString if you've already created a string called myString.

Share this post


Link to post
Share on other sites
Using a dictionary would look something like:

Dictionary<string, Player> dict = new Dictionary<string,Player>();
dict["127.0.0.1"] = new Player();

For the most part you just use the string inside the []s whenever you want to lookup an entry in the Dictionary.

Share this post


Link to post
Share on other sites
I've got it:

Dictionary<IPEndPoint, Player> dPlayerDict = new Dictionary<IPEndPoint, Player>();

public void Add(IPEndPoint ip, string sPlayerID, int iPlayerX, int iPlayerY, int iOutfit)
{
dPlayerDict.Add(ip, new Player(sPlayerID, iPlayerX, iPlayerY, iOutfit));
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this