I can not add in Hashtable

First, sorry for my bad english.

I have this:

public static Users server = new Users();

static void Main(string[] args)
{
    server.addNewUser(1, "John");
    server.addNewUser(2, "Marlon");
    server.addNewUser(3, "Lucas");
    Console.WriteLine(server.countUsers()); //COUNT USERS

    TcpManager manager = new TcpManager(58636, 500);

    Console.Read();
}

TcpManager:

public Users test = new Users();
internal TcpManager(int port, int maxuserson)
{
    test.addNewUser(4, "Julio");
    test.addNewUser(5, "Marcelo");
    test.addNewUser(6, "Andre");
    Console.WriteLine(test.countUsers()); //COUNT USERS
}

and my Users class:

class Users
{
    Hashtable users = new Hashtable();

    public void addNewUser(int id, string name)
    {
        if (!users.ContainsKey(id))
            users.Add(id, name);
        else users[id] = name;
    }

    public int countUsers()
    {
        return users.Count;
    }
}

My problem is that when I add a User on Main, it works! But when I try to do the same in TcpManager, he simply does not add.

How to make it work? I tried with List, Dictionary, Tuple, all with same problem.

My first ConsoleWrite return "3", and second in TcpManager return "3" too, when should return "6".

Thank you all, and again, sorry for my English.

Upvotes: 0

Views: 62

Answers (2)

nozzleman
nozzleman

Reputation: 9649

You declared the Hashtable in the Users class as an instance-field. this means every instance of Users has its own hashtable. Therefore, you program behaves just as one would expect.

There are several ways to make it work the way you want.

Make Users static

static class Users
{
    private static Hashtable users;

    static Users()
    {
       users = new Hashtable();
    }

    public static void addNewUser(int id, string name)
    {
        if (!users.ContainsKey(id))
            users.Add(id, name);
        else users[id] = name;
    }

    public static int countUsers()
    {
        return users.Count;
    }    
}

Usage

Users.addNewUser(1, "Vera");
Console.WriteLine(Users.countUsers());

Make use users Hashable static (Essentially, this is the Monostate-Pattern)

class Users
{
    private static Hashtable users = new HashTable();

    public void addNewUser(int id, string name)
    {
        if (!users.ContainsKey(id))
            users.Add(id, name);
        else users[id] = name;
    }

    public int countUsers()
    {
        return users.Count;
    }    
}

Usage

var users = new Users();
users .addNewUser(1, "Vera");
Console.WriteLine(users.countUsers());

Use Singleton

class Users
{
    private Hashtable users;

    private static Users instance;

    private Users()
    {
         users = new HashTable();
    }

    public static Users Instance 
    {
        get
        {
            if (instance == null)
                instance=new Users();
            return instance;
        }
    }

    public void addNewUser(int id, string name)
    {
        if (!users.ContainsKey(id))
            users.Add(id, name);
        else users[id] = name;
    }

    public int countUsers()
    {
        return users.Count;
    }    
}

Usage

Users.Instance is the only instance of users that will ever be created, since the ctor is private and only used in the Instance-Property.

var users = Users.Instance;
users.addNewUser(1, "Vera");
users.countUsers();

Upvotes: 1

SlaneR
SlaneR

Reputation: 714

try with this way:

TcpManager manager = new TcpManager(58636, 500);

test.addNewUser(4, "Julio");
test.addNewUser(5, "Marcelo");
test.addNewUser(6, "Andre");

Console.WriteLine(manager.test.countUsers()); // 6

in TcpManager constructor:

internal TcpManager(int port, int maxuserson)
{
    // This code will executed when you call  new TcpManager();
    // So, you have to add 1, 2 and 3 ids first.
    manager.test.addNewUser(1, "John");
    manager.test.addNewUser(2, "Marlon");
    manager.test.addNewUser(3, "Lucas");
    Console.WriteLine(test.countUsers()); //COUNT USERS: 3
}

Upvotes: 1

Related Questions