我想將一些代碼放在另一個.cs文件中,因爲我使用了 一個巨大的switch語句,我不希望人們通過它來了解了解正在發生的事情。問題是描述在我的應用程序中寫入的文本框的txtresponse 命令不是在新文件中定義的 ,我無法弄清楚原因。這是我的新 文件:鏈接我的.cs文件
(Error: txtresponse does not exist in this current context)
附:只使用System.Windows.Forms;突出顯示它擁有所有的應用形式, 按鈕和他們做什麼等等......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GUI_Verify
{
class commands : Form
{
public static string call_command(string stmnt)
{
string cmd = "";
switch (stmnt)
{
case "TST_ALL":
txtresponse.Text = "\r\nTesting all memory tests.\r\n";
cmd = "1";
break;
case "TST_PKT_BFFR":
txtResponse.Text = "\r\nTest for Packet Buffer.\r\n";
cmd = "2";
break;
case "TST_XDATA":
txtResponse.Text = "\r\nTest for XDATA.\r\n";
cmd = "3";
break;
case "TST_REN_RAM":
txtResponse.Text = "\r\nTest for Retention Ram.\r\n";
cmd = "4";
break;
case "TST_CODE_RAM":
txtResponse.Text = "\r\nTest for Code Ram.\r\n";
cmd = "5";
break;
case "Copy_All":
txtResponse.Text = "\r\nCopying all memory to all places.\r\n";
cmd = "6";
break;
case "to_xdata":
txtResponse.Text = "\r\nCopying xdata -> xdata.\r\n";
cmd = "7";
break;
default:
txtResponse.Text = "\r\nunknown command\r\n";
break;
}
return cmd;
}
}
}
您正在繼承通用表單,而不是包含該文本框的表單。 –
如果所有switch語句都將某些字符串映射到整數,則應該使用字典來查看 –
您確定要讓call_command爲靜態嗎? –