我需要在类内部的方法中更改类变量,我该怎么做?
class MainGame
{
public string Connected_IP = " ";
public short Is_Connected = 0;
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.Run(new MainConsole());
MainConsole m = new MainConsole();
}
public ref string checkCommands(string command)
{
IP_DataBase ips = new IP_DataBase();
/*checking for ips in the list*/
string[] dump;
if (command.Contains("connect"))
{
dump = command.Split(' ');
for (int i = 0; i < ips.IPS.Length; i++)
{
if (dump[1] == ips.IPS[i])
{
Connected_IP = dump[1];
Is_Connected = 1;
break;
}
else
{
Connected_IP = "Invalid IP";
Is_Connected = 0;
}
}
}
else if (command.Contains("quit")) /*disconnect command*/
{
Connected_IP = "Not Connected";
Is_Connected = 0;
}
return ref Connected_IP;
}
}
我希望 Connected_IP 会改变。但是相反,变量只是在方法中发生了变化,我尝试使用 ref 命令但仍然没有变化,我该怎么办?
public partial class MainConsole : Form
{
MainGame m = new MainGame();
MainGame j = new MainGame();
private void ConsoleInput2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
Text_IP_Connected.Text = m.checkCommands(ConsoleInput2.Text);
if (m.Is_Connected == 1) vic_sft.Enabled = true;
else vic_sft.Enabled = false;
}
}
阿晨1998
相关分类