是一种在给定上下文中无效的方法

时间:2013-06-03 09:28:06

标签: c# chat

我的代码:

public ChatServer()
        {
            //create our nickname and nickname by connection variables
            nickName = new Hashtable(100);
            nickNameByConnect = new Hashtable(100);
            //create our TCPListener object
            chatServer = new System.Net.Sockets.TcpListener(4296);
            //check to see if the server is running
            //while (true) do the commands
            while (true)
            {
                chatServer.Start();
                if (chatServer.Pending())
                {
                    Chat.Sockets.TcpClient chatConnection = chatServer.AcceptTcpClient();
                    Console.WriteLine("You Are Connected");
                    DoCommunicate com = new DoCommunicate(chatConnection);
                }
            }
        }

    public static void SendMsgToAll(string nick, string msg)
    {

        StreamWriter writer;
        ArrayList remove = new ArrayList(0);
        Chat.Sockets.TcpClient[] tcpClient = new Chat.Sockets.TcpClient[ChatServer.nickName.Count];
        ChatServer.nickName.Values.CopyTo(tcpClient, 0);

从上面的代码中,我们可以看到i-ChatServer ii-SendMsgToAll有2种方法。 所以当我尝试在第二种方法中编写行Chat.Sockets.TcpClient[] tcpClient = new Chat.Sockets.TcpClient[ChatServer.nickName.Count];

它出现以下错误:

ChatSerevr()是一种在给定上下文中无效的方法

我谷歌但无法找到正确的解决方案。提前致谢

2 个答案:

答案 0 :(得分:3)

重命名ChatServer()方法,或重命名ChatServer类型(或属性)。编译器认为,您在此处使用了一种方法:ChatServer.nickName.Count

答案 1 :(得分:-1)

我可能有点晚了......

请确保您没有与您的班级同名的方法(当然,构造函数不适用)