问题描述
嗨
我有两个名为ChatServer和ChatClient的项目.当用户单击登录按钮时,我利用事件和委托来引发事件.
ChatServer上的代码是:
Hi
I have two projects named ChatServer and ChatClient. I make use of events and delegats to raise the event when user clicks on login button.
The code on ChatServer is:
public class ChatServerClass : IServiceInterface
{
public delegate void UserJoin(string username);
public event UserJoin Newjoin;
public void JoinChat(string name)
{
if (flag == true)
{
ListofUsers.Add(name);
MessageBox.Show("User Logged in");
Newjoin(name);
}
else
MessageBox.Show("Select other Name");
ListofUsers.ToArray();
}
ChatClient上的代码是
The code on ChatClient is
private void login_btn_Click(object sender, EventArgs e)
{
//Client newclient = new Client(login_txt.Text);
username = login_txt.Text;
//InstanceContext context = new InstanceContext(Icall);
//connect.Open();
client.JoinChat(username);
ChatServerClass cs=new ChatServerClass();
cs.Newjoin+=new Userjoin(UserEnter);
public void UserEnter(string username)
{
MessageBox.Show("User" +username + "is online");
}
在ChatServer中添加了对ChatClient的引用.我的意思是我在ChatServer项目中使用ChatClient参考添加.当我单击登录按钮时,出现异常,提示未将tat对象引用设置为实例.
请帮助
The reference to ChatClient is added in ChatServer. I mean i am adding using ChatClient reference in ChatServer project. When i click on the login button i get exception saying tat object reference not set to the instance.
PLease help
推荐答案
if (flag == true)
{
ListofUsers.Add(name);
MessageBox.Show("User Logged in");
UserJoin handler = Newjoin; // this is not necesarry, it just
if(handler != null) // to make it threadsafe.
handler (name);
}
client.JoinChat(username);
实例化了客户端对象吗?究竟是什么对象?
干杯...
have you instantiated the client object?? what object exactly its meant to be?
Cheers...
这篇关于对象引用未设置为实例-事件和委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!