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

本文关键字:参数传递 是否 同一个 初始化 | 更新日期: 2023-09-27 18:33:34

Class:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession)]
public class MainService : IChat
{
    IChatCallback ChatCallback = OperationContext.Current.GetCallbackChannel<IChatCallback>();
    Chat chat = new Chat(this);
    public void ShowChat()
    {
        chat.Show();
    }
    public void SendInstantMessage(string user, string message)
    {
        chat.RaiseMsgEvents(user, message);
        ChatCallback.InstantMessage(user, message);
    }
 }

形式:

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

mainForm 像这样使用类:

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

主窗体中,我只是传递类,没有初始化,但是在类中,当调用ShowChat()时,我需要显示聊天窗体并进入这个类方法,以便我可以发送消息。

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

.

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

您得到的错误是因为您在全局级别使用"this"实例化对象。

更新

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

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

这只是个人的烦恼,但拥有与全局变量同名的函数参数是......好吧,对我来说不行。 我在你的Chat.Chat(MainService)功能上注意到了这一点。

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

正如其他帖子所建议的那样,您需要重新考虑如何在example类中实例化chat字段。我会考虑懒惰加载属性,就像这样...

private ChatForm _Chat = null;
private ChatForm Chat
{
    get
    {
        if (this._Chat == null)
        {
            this._Chat = new ChatForm(this);
        }
        return this._Chat;
    }
    set { this._Chat = value; }
}

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