【问题标题】:Is a method which is not valid in the given context是在给定上下文中无效的方法
【发布时间】:2013-09-24 23:18:55
【问题描述】:

我的代码:

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);

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

它给出了以下错误:

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

我用谷歌搜索但找不到正确的解决方案。提前致谢

【问题讨论】:

  • ChatSerevr() != CharServer() 错字还是?
  • 我不清楚你的评论。你能详细说明一下吗?
  • 编译器抛出该错误的行在哪里?

标签: c# chat


【解决方案1】:

要么重命名ChatServer() 方法,要么重命名ChatServer 类型(或属性)。编译器认为,您在这里使用了一个方法:ChatServer.nickName.Count

【讨论】:

  • ChatServer 实际上是我相信的一个类。那么 nickName 是一个字段或一个属性。
  • @aiaptag:对不起,我的评论是针对 OP。 :) 错过了昵称。
【解决方案2】:

我可能有点晚了……

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 2018-05-07
    • 2020-04-16
    相关资源
    最近更新 更多