改变类变量永久 C#

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


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;           

            }

        }


波斯汪
浏览 47回答 1
1回答

阿晨1998

在下面的示例中,我在 MainGame 类本身中创建并存储了一个 MainGame 实例。因为这是从静态 Main() 完成的,所以声明也必须是静态的。请注意,如果进行了此声明public,则可以使用语法从任何地方访问它MainGame.mg(但是,这不是推荐的方法)。接下来,我们通过该行中的 Constructor 将该 MainGame 实例传递给MainConsole表单Application.Run()。请注意下面发布的 MainConsole 中的附加构造函数。返回类型中的“ref”checkCommands()已被删除,因为可以在 MainConsole 本身中使用传递和存储的对 MainGame 的引用更改该值。主游戏类:public class MainGame{&nbsp; &nbsp; public string Connected_IP = " ";&nbsp; &nbsp; public short Is_Connected = 0;&nbsp; &nbsp; static MainGame mg = null; // instantiated in Main()&nbsp; &nbsp; static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; mg = new MainGame(); // this instance will be worked with throughout the program&nbsp; &nbsp; &nbsp; &nbsp; Application.EnableVisualStyles();&nbsp; &nbsp; &nbsp; &nbsp; Application.SetCompatibleTextRenderingDefault(false);&nbsp; &nbsp; &nbsp; &nbsp; Application.Run(new MainConsole(mg)); // pass our reference of MainGame to MainConsole&nbsp; &nbsp; }&nbsp; &nbsp; public string checkCommands(string command) // no "ref" on the return type&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; IP_DataBase ips = new IP_DataBase();&nbsp; &nbsp; &nbsp; &nbsp; /*checking for ips in the list*/&nbsp; &nbsp; &nbsp; &nbsp; string[] dump;&nbsp; &nbsp; &nbsp; &nbsp; if (command.Contains("connect"))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dump = command.Split(' ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < ips.IPS.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dump[1] == ips.IPS[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connected_IP = dump[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Is_Connected = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connected_IP = "Invalid IP";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Is_Connected = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (command.Contains("quit")) /*disconnect command*/&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connected_IP = "Not Connected";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Is_Connected = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return Connected_IP;&nbsp; &nbsp; }}在这里,在 MainConsole 表单中,我们添加了一个额外的构造函数来接收 MainGame 的实例。有一个名为mMainGame 类型的字段,但请注意,在这种形式中,我们实际上没有使用“new”创建 MainGame 的实例;我们只使用传入的实例。对 MainGame 的引用存储在m构造函数中,以便它可以在代码的其他点使用:public partial class MainConsole : Form{&nbsp; &nbsp; // Note that we are NOT creating an instance of MainGame anywhere in this Form!&nbsp; &nbsp; private MainGame m = null; // initially null; will be set in Constructor&nbsp; &nbsp; public MainConsole()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; }&nbsp; &nbsp; public MainConsole(MainGame main)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; this.m = main; // store the reference passed in for later use&nbsp; &nbsp; }&nbsp; &nbsp; private void ConsoleInput2_KeyDown(object sender, KeyEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (e.KeyCode == Keys.Return && ConsoleInput2.Text.Trim().Length > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Use the instance of MainGame, "m", that was passed in:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text_IP_Connected.Text = m.checkCommands(ConsoleInput2.Text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vic_sft.Enabled = (m.Is_Connected == 1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP