Visual C# - Access instance of object created in one class in another

前端 未结 4 806
Happy的楠姐
Happy的楠姐 2020-12-08 16:54

I apologize in advance with what will probably be a fairly easy/quick answer based on scope, but I\'ve looked everywhere and am surprised to not come up with an answer.

4条回答
  •  一个人的身影
    2020-12-08 17:28

    You can also make Soldier a static variable :

    public static Soldier soldier {get;set;}
    private void button1_Click(object sender, EventArgs e)
    {
        soldier = new Soldier();
        soldier.surname = textbox1.text;
    }
    

    Code in other forms:

    Form1.soldier.name ="";
    

提交回复
热议问题