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

时间:2012-05-09 15:58:13

标签: c# class arguments

类别:

[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()调用时我需要显示聊天表单并获取此类方法以便我可以发送消息。

3 个答案:

答案 0 :(得分:1)

.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)函数中注意到了这一点。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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