简体   繁体   English

从Windows窗体应用程序获取值,并将其传递回main方法

[英]Taking values from a windows form application, and passing them back to the main method

I am working on my final project, and I have a hit a wall. 我正在完成我的最后一个项目,但遇到了麻烦。 The arrays that are created and defined from the Form are being sent to the Main method, but I cannot further alter them later. 从窗体创建和定义的数组将被发送到Main方法,但是以后我无法再对其进行更改。

public string[] ShipTypes
    {
        get
        {
            string[] ships = new string[6];
            ships[0] = "Galaxy Class";
            ships[1] = "Galaxy Class";
            ships[2] = "Galaxy Class";
            ships[3] = "Galaxy Class";
            ships[4] = "Galaxy Class";
            ships[5] = "BattleCruiser";
            return ships;

        }
    }

but after I go to change the values 但是在我更改值之后

public void verifyButton_Click(object sender, EventArgs e) {
if (shipSelected1 == birdOfPrey || shipSelected1 == battleCruiser)
        {
            kShips++;
            if (shipSelected1 == birdOfPrey)
            {
                birdCount++;
                ShipTypes;
                ShipNames[0] = name1.Text;
                ShipShields[0] = shieldValue1;

            }
            else
            {
                battleCount++;
                ShipTypes[0] = battleCruiser;
                ShipNames[0] = name1.Text;
                ShipShields[0] = shieldValue1;
            }
        }

nothing happens at all, it keeps the original values from the initalization 完全没有任何反应,它避免了初始值的初始化

ShipTypes's get definition guarantees that, regardless of what you set the value of that array, return that local ships array you created. ShipTypes的get清晰的保证,不管你设置阵列的价值,返回本地ships阵列创建。 Change your get . 改变你的get

* When accessing a property, the get body is executed. *访问属性时,将执行get主体。 When passing a value to a property, the set body is executed. 将值传递给属性时,将执行set主体。 Within the ShipTypes's get body, that ships array is created and returned. 在ShipTypes的get主体内,创建并返回了该ships数组。 You are not getting returning what you need. 您没有得到想要的东西。

I would define: 我将定义:

public string[] _shipTypes;
public string[] ShipTypes
{
    get
    {
        if (_shipTypes == null)
        {
            _shipTypes = new string[6];

            _shipTypes[0] = "Galaxy Class";
            _shipTypes[1] = "Galaxy Class";
            _shipTypes[2] = "Galaxy Class";
            _shipTypes[3] = "Galaxy Class";
            _shipTypes[4] = "Galaxy Class";
            _shipTypes[5] = "BattleCruiser";

            return _shipTypes;
        }
        else return _shipTypes;
    }
    set => _shipTypes = value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM