在c#中的表单之间传递对象

时间:2014-04-04 08:51:29

标签: c# forms object

HY,

我有以下课程:

 public class Player
{
    private int score;
    private int mapSize;
    private int wins;
    private int level;
    private int plays;
   // private List<String> Paths { get; set; }
    public String Paths { get; set; }

    public Player()
    {
    }

    public Player(String name)
    {
        Name = name;
    }

    public Player(String name, int score)
    {
        Name = name;
        this.score = score;
    }

    public Player(String name, String Paths, int score, int wins, int plays)
    {
        Name = name;
        this.Paths = Paths;
        this.score = score;
        this.wins = wins;
        this.plays = plays;

    }

    public int Score
    {
        get
        {
            return score;
        }

        set
        {
            score = value;
        }

    }

    public int MapSize
    {
        get
        {
            return mapSize;
        }
        set
        {
            mapSize = value;
        }
    }

    public String Name { get; set; }

...

和一个表单中的一个事件处理程序

private void NewGame_Click(object sender, EventArgs e)
{
    MainWin ng = new MainWin(this.Name);

    this.Hide();
    ng.ShowDialog();
    this.Close();

}

我尝试将对象的名称发送给另一种形式

public MainWin(Player name)
{
    InitializeComponent();
    this.Name = name;
    //nXNToolStripMenuItem
}

但是我得到了错误名称:Cannot implicitly convert type Player to string

请帮忙,我如何将对象名称的适当性从一种形式发送到另一种形式?

1 个答案:

答案 0 :(得分:2)

this引用按钮处理程序中的当前表单对象,this.Name是一个字符串,MainWin构造函数Form期望Player对象,所以你需要传递{ {1}}类对象

Player