C# 当同一个类已初始化时,是否可以将该类作为参数传递?

C# 当同一个类已初始化时,是否可以将该类作为参数传递?,c#,class,arguments,C#,Class,Arguments,类别: 主窗体使用如下类: public partial class Chat : Form { MainService service; public Chat(MainService service) { InitializeComponent(); OnMsgReceivedEvent += new OnMsgReceived(callback_OnMsgReceivedEvent); this.service =

类别:

主窗体使用如下类:

public partial class Chat : Form
{
    MainService service;

    public Chat(MainService service)
    {
        InitializeComponent();
        OnMsgReceivedEvent += new OnMsgReceived(callback_OnMsgReceivedEvent);
        this.service = service;
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        service.SendInstantMessage("Admin", txtMsg.Text);
    }
 }

在主窗体中,我只传递类,不进行初始化,但在调用
ShowChat()
时,我需要显示聊天窗体并访问此类方法,以便发送消息。

当然是这样,只需创建一个方法,将您的类作为参数并调用它…

NET是一种面向对象的语言。事实上,每个类都是一个对象

您得到的错误是因为您正在全局级别上用“this”实例化一个对象

更新 根据您的更新,您可以执行以下操作,它将起作用。您可能需要进一步重构它,以确保它不会违反任何业务规则等

 public partial class Form1 : Form
 {
    ServiceHost host;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        host = new ServiceHost(typeof(WCF_Server.MainService));
        host.Open();
    }
 }
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple,InstanceContextMode=InstanceContextMode.PerSession)]
公共类主要服务:IChat
{
IChatCallback ChatCallback=OperationContext.Current.GetCallbackChannel();
//将此更改为仅声明。此将为空,
//由于还没有对象,这实际上只是一个指向虚无的指针。
//这告诉系统您可能/正计划使用一个名为
//聊天,但它还不存在。
聊天;
//启动默认构造函数。这将创建实际的聊天对象,允许类的其他成员访问它。
公共服务()
{
//实例化它!(或者我们的一些合作伙伴说“我们正在更新它”)
chat=新聊天(此);
}
//现在聊天已经被实例化/创建,您可以使用它了。
公共void ShowChat()
{
chat.Show();
}
public void SendInstantMessage(字符串用户,字符串消息)
{
chat.RaiseMsgEvents(用户、消息);
InstantMessage(用户,消息);
}
}

这只是个人的不满,但如果函数参数与全局变量同名,则。。。对我来说没有。我在你的聊天功能中注意到了这一点

正如其他帖子所建议的,您需要重新考虑如何在
示例
类中实例化
聊天
字段。我会考虑延迟加载属性,比如……/p>
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession)]
public class MainService : IChat
{
    IChatCallback ChatCallback = OperationContext.Current.GetCallbackChannel<IChatCallback>();
    //Changed this to be just a declaration. This will be null,
    // as there is no object yet, this is really just a pointer to nothing.
    //This tells the system that you might/are planning to use an object called 
    //chat, but it doesn't exist yet.
    Chat chat;

    // Get your default constructor going. This will create the actual chat object, allowing the rest of your class to access it.
    public MainService()
    {
         //instantiate it! (or as some of our co-ops say "We're newing it")
         chat = new Chat(this);
    }

    //now that chat is actually instantiated/created you can use it.
    public void ShowChat()
    {
        chat.Show();
    }
    public void SendInstantMessage(string user, string message)
    {
        chat.RaiseMsgEvents(user, message);
        ChatCallback.InstantMessage(user, message);
    }
 }

使用延迟加载将确保您能够根据请求使用关键字
this

您遇到了什么样的错误?@skyfoot yea,我得到了“关键字“this”在当前上下文中不可用”您正试图从构造函数访问
this
<代码>此尚未初始化。@MurHafSoz:您发布的代码将在将类
Chat
更改为
ChatForm
后编译。因此,您的代码必须与您发布的代码不同。@JonSkeet很抱歉,我编辑了代码。非常感谢,我是否应该删除问题中的第一个错误部分?由您决定。这可能会进一步澄清这个问题,以帮助未来的人们。
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession)]
public class MainService : IChat
{
    IChatCallback ChatCallback = OperationContext.Current.GetCallbackChannel<IChatCallback>();
    //Changed this to be just a declaration. This will be null,
    // as there is no object yet, this is really just a pointer to nothing.
    //This tells the system that you might/are planning to use an object called 
    //chat, but it doesn't exist yet.
    Chat chat;

    // Get your default constructor going. This will create the actual chat object, allowing the rest of your class to access it.
    public MainService()
    {
         //instantiate it! (or as some of our co-ops say "We're newing it")
         chat = new Chat(this);
    }

    //now that chat is actually instantiated/created you can use it.
    public void ShowChat()
    {
        chat.Show();
    }
    public void SendInstantMessage(string user, string message)
    {
        chat.RaiseMsgEvents(user, message);
        ChatCallback.InstantMessage(user, message);
    }
 }
private ChatForm _Chat = null;
private ChatForm Chat
{
    get
    {
        if (this._Chat == null)
        {
            this._Chat = new ChatForm(this);
        }
        return this._Chat;
    }
    set { this._Chat = value; }
}