![](/img/trans.png)
[英]How to call a non-static method in an imported project (from another non-static method) in Visual Studio
[英]How to call a non-static method
在我的“ Server.cs”類中,我想從表單訪問文本框。 我嘗試使用此代碼:
ServerWindow.WriteChatlog(encoder.GetString(message, 0, bytesRead));
但是我得到了這個錯誤:
An object reference is required for the non-static field, method, or property 'Chat_Server.ServerWindow.WriteChatlog(string)
ServerWindows.WriteChatlog(字符串文本)如下所示:
public void WriteChatlog(string text)
{
textBox1.Text += text + Environment.NewLine;
}
現在,我嘗試直接從使用ServerWindow.WriteChatlog方法的位置執行此操作,但此方法不起作用。
它們都是非靜態的
您需要在ServerWindow
類的實例上調用WriteChatLog
。 例如:
ServerWindow servwin = new ServerWindow();
servwin.WriteChatlog(encoder.GetString(message, 0, bytesRead));
您必須使用this.WriteChatlog(...)
或僅使用WriteChatlog(...)
。 假定您正在從表單的代碼中調用代碼。
調用ServerWindow.WriteChatlog
時,沒有ServerWindow
的實際實例。 該方法可能需要是靜態的: public static void WriteChatlog(string text)
或您需要ServerWindow的實例: ServerWindow serverWindow = new ServerWindow()
我懷疑前者是您真正需要的。
就像控制台上沒有實例方法一樣,一切都是靜態的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.