簡體   English   中英

私有類變量在表格之間保持重置

[英]Private class variables keep resetting between forms

我用c#創建了一個簡單的井字游戲,到目前為止,我只有2種形式。

  • frmMain. (主要游戲形式)
  • frmPlayerInfo. (用於選擇一個或兩個玩家)

我還創建了一個類Player。

所以我雖然是這個 一旦玩家輸入了他們的名字,它將進入Player類中的一個屬性,並將值存儲在私有變量中,這樣我就可以在主窗體上獲取它以進行顯示。 但是,當我回到主要形式時,屬性又返回了null,這是為什么呢?

這是我的代碼。

播放器類...(我已刪除無關的屬性和與此問題無關的變量)

class Player
{
    #region PrivatVariables
    private string _PlayerName1;
    private string _PlayerName2;
    #endregion

    #region Properties
    public string playerName1
    {
        get
        {
            return _PlayerName1;
        }
        set
        {
            _PlayerName1 = value;
        }
    }

    public string playerName2
    {
        get
        {
            return _PlayerName2;
        }
        set
        {
            _PlayerName2 = value;
        }
    }
    #endregion


    public Player()
    {
        // Do nothing
    }

    internal void resetValues()
    {
    }
}

Main Form ...(我只包含了主要代碼,在加載窗體時調用了新的游戲代碼)

public partial class frmMain : Form
{
    Player player = new Player();

    public frmMain()
    {
        InitializeComponent();
    }

    private void frmMain_Load(object sender, EventArgs e)
    {
        // TODO : Set timer so that form can load first.

        newGame();
    }

    private void newGame()
    {
        // Creating an instance of the Form used to take in player info.
        frm_PlayerInfo playerInfo = new frm_PlayerInfo();
        // This is  going to pop up the player info form.
        DialogResult dialogResult = playerInfo.ShowDialog(); 
        LoadForm();
    }

    private void LoadForm()
    {
        grpBoxPlayer1.Text = player.playerName1;
    }

玩家信息表...()

 public partial class frm_PlayerInfo : Form
{
    Player player = new Player();

    bool isAnimated;

    public frm_PlayerInfo()
    {
        InitializeComponent();
    }

    private void frm_PlayerInfo_Load(object sender, EventArgs e)
    {
        // Setting player tow input visibility to false because there will always be one player.
        this.txtPlayerName2.Visible = false;
        this.lblPlayerName2.Visible = false;
    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        btnNext.Visible = false;

        // Used to slide the Form up or down.
        slideAnimation(ref isAnimated);
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        // Populating the Player Properties
        player.playerName1 = this.txtPlayerName1.Text;


        if (this.txtPlayerName2.Text != "")
        {
            player.playerName2 = this.txtPlayerName2.Text;
        }

        // Calling the animation method to close the animation back up and then the form will be closed.
        slideAnimation(ref isAnimated);
        FadeOutAnimation();
    }

在frmMain中創建Player類的實例,然后將其傳遞給構造函數中的frmplayerInfo。 現在,您在frmMain中沒有對Player類的引用。

newGame()方法中:

private void newGame()
{
    Player player = new Player();
    // Creating an instance of the Form used to take in player info.
    frm_PlayerInfo playerInfo = new frm_PlayerInfo(player);
    // This is  going to pop up the player info form.
    DialogResult dialogResult = playerInfo.ShowDialog(); 
    LoadForm();
}

並在frm_PlayerInfo類中,刪除以下代碼行: Player player = new Player(); ,並更改其構造函數:

public partial class frm_PlayerInfo : Form
{
Player player;

public frm_PlayerInfo(Player player)
{
    InitializeComponent();
    this.player = player;
}
// the rest of the form

另一種選擇是直接在frmPlayerInfo中具有播放器名稱屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM