【问题标题】:Is it possible to pass a class as an argument when the same class has initialized?当同一个类初始化时,是否可以将一个类作为参数传递?
【发布时间】:2012-05-18 04:22:39
【问题描述】:

类:

[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()的类中,我需要显示聊天窗体并访问该类方法,以便我可以发送消息。

【问题讨论】:

  • 你遇到什么样的错误?
  • @skyfoot 是的,我得到“关键字 'this' 在当前上下文中不可用”
  • 您正在尝试从您的构造函数访问thisthis 尚未初始化。
  • @MurHafSoz:将Chat 类更改为ChatForm 后,您发布的代码将编译。因此,您的代码必须与您发布的不同。
  • @JonSkeet 抱歉,我编辑了代码。

标签: c# class arguments


【解决方案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) 函数中注意到了这一点。

【讨论】:

  • 非常感谢,我应该从我的问题中删除第一个错误的部分吗?
  • 由你决定。它可能会更清楚地解决这个问题,以帮助未来的人们。
【解决方案2】:

正如其他帖子所建议的那样,您需要重新考虑如何在 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

【讨论】:

    【解决方案3】:

    当然可以,只要创建一个以你的这个类为参数的方法并调用它...

    【讨论】:

      猜你喜欢
      • 2017-02-27
      • 2021-10-02
      • 2023-01-18
      • 2012-10-18
      • 1970-01-01
      • 2016-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多