如何在重载中传递Class实例

时间:2013-01-14 16:56:30

标签: c# class object

我有Character

中实例化的类Player的一组对象(Form1

我需要以另一种形式(Combat)使用这些对象,但我不知道如何传递它们。这是我尝试过的:

public partial class Form1: Form
 private void combatBtn_Click(object sender, EventArgs e)
    {
        Form combat = new Combat(Character[0-9]);

        combat.ShowDialog();

    }

public partial class Combat : Form
  {

    Player[] character = new Player[9];

    public Combat(Player chars[0-9])
    {
        InitializeComponent();

        for (int i = 0; i > 9; i++)
        {
            character[i] = chars[i];
        }
    }

1 个答案:

答案 0 :(得分:5)

您可以将属性添加到Combat并从父表单设置:

public partial class Combat : Form
{
    public Player[] Characters { get; set; }
}

然后,您可以在创建表单时设置该属性:

private void combatBtn_Click(object sender, EventArgs e)
{
    Combat combat = new Combat();
    combat.Characters = somePlayerArray;

    combat.ShowDialog();
}