C# 是在给定上下文中无效的方法
我的代码:C# 是在给定上下文中无效的方法,c#,chat,C#,Chat,我的代码: public ChatServer() { //create our nickname and nickname by connection variables nickName = new Hashtable(100); nickNameByConnect = new Hashtable(100); //create our TCPListener object
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。
因此,当我尝试编写lineChat.Sockets.TcpClient[]TcpClient=new Chat.Sockets.TcpClient[ChatServer.昵称.Count]第二种方法中的code>
它给出了以下错误:
ChatSerevr()是在给定上下文中无效的方法
我用谷歌搜索它,但找不到正确的解决方案。提前感谢重命名ChatServer()
方法,或者重命名ChatServer
类型(或属性)。编译器认为,您在这里使用的是一种方法:ChatServer.nickName.Count
我可能有点晚了
请确保没有与类同名的方法(构造函数当然不适用)ChatSerevr()
!=CharServer()
typo或?我不清楚您的评论。你能解释一下吗?编译器抛出错误的那一行在哪里?我相信ChatServer实际上是一个类。那么昵称就是一个字段或属性。@aiapatag:对不起,我的评论是针对OP.:)遗漏的昵称。