为了账号安全,请及时绑定邮箱和手机立即绑定

改变类变量永久 C#

改变类变量永久 C#

C#
波斯汪 2022-11-21 17:00:55
我需要在类内部的方法中更改类变量,我该怎么做?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 回答

?
阿晨1998

TA贡献2037条经验 获得超6个赞

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


接下来,我们通过该行中的 Constructor 将该 MainGame 实例传递给MainConsole表单Application.Run()。请注意下面发布的 MainConsole 中的附加构造函数。返回类型中的“ref”checkCommands()已被删除,因为可以在 MainConsole 本身中使用传递和存储的对 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 的实例。有一个名为mMainGame 类型的字段,但请注意,在这种形式中,我们实际上没有使用“new”创建 MainGame 的实例;我们只使用传入的实例。对 MainGame 的引用存储在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);

        }

    }


}


查看完整回答
反对 回复 2022-11-21
  • 1 回答
  • 0 关注
  • 46 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信