更改类变量永久C#

时间:2019-03-30 12:55:19

标签: c# winforms class variables methods

我需要在类内部的方法中更改类变量, 我该怎么办?

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;           
            }
        }

1 个答案:

答案 0 :(得分:0)

在下面的示例中,我正在MainGame类本身中创建并存储MainGame的实例。因为这是通过静态Main()完成的,所以声明也必须是静态的。请注意,如果声明为public,则可以使用语法MainGame.mg从任何地方访问它(但是,不建议这样做)。

接下来,我们通过Application.Run()行中的 Constructor 将MainGame的实例传递给MainConsole表单。请注意下面发布的MainConsole中的其他构造函数。 checkCommands()中返回类型上的“ ref”已被删除,因为可以使用在MainConsole本身中传递和存储的对MainGame的引用来更改值。

MainGame类:

public class MainGame
{

    public string Connected_IP = " ";
    public short Is_Connected = 0;

    static MainGame mg = null; // instantiated in Main()

    static void Main()
    {
        mg = new MainGame(); // this instance will be worked with throughout the program
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainConsole(mg)); // pass our reference of MainGame to MainConsole
    }

    public string checkCommands(string command) // no "ref" on the return type
    {
        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 Connected_IP;
    }

}

在这里,我们以MainConsole形式添加了一个额外的构造函数,该构造函数接收MainGame的实例。有一个名为m的字段,类型为MainGame,但请注意,这种形式的无处实际上是我们使用“ new”创建MainGame的实例。我们仅使用传入的实例。对MainGame的引用存储在Constructor的m中,以便可以在代码中的其他地方使用:

public partial class MainConsole : Form
{

    // Note that we are NOT creating an instance of MainGame anywhere in this Form!

    private MainGame m = null; // initially null; will be set in Constructor

    public MainConsole()
    {
        InitializeComponent();
    }

    public MainConsole(MainGame main)
    {
        InitializeComponent();
        this.m = main; // store the reference passed in for later use
    }

    private void ConsoleInput2_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Return && ConsoleInput2.Text.Trim().Length > 0)
        {
            // Use the instance of MainGame, "m", that was passed in:
            Text_IP_Connected.Text = m.checkCommands(ConsoleInput2.Text);
            vic_sft.Enabled = (m.Is_Connected == 1);
        }
    }

}