制作Windows窗体棋盘游戏,用户可以选择难度。此代码将检查用户是否已选择难度,然后将此难度写入属性。
if (combo_difficulty.SelectedIndex > -1)
{
Hide();
//Main Menu form closes where user selects difficulty
new Game().Show();
//Game form where user player actual game
Form2.playerStudent.Difficulty = combo_difficulty.Text;
//This is a test and it outputs the difficulty as ecpected
MessageBox.Show(Form2.playerStudent.Difficulty);
}
这是显示我所指的属性的学生班。
public class Student : Player
{
private int _score;
private string _difficulty;
public Student(string username, string password, int score) : base(username, password)
{
_score = Score;
}
public string Difficulty
{
get { return _difficulty; }
set { _difficulty = value; }
}
但是当我想在游戏形式中阅读所选择的难度时,不会返回任何值。我希望此消息显示用户在以前的表单中选择的难度。
public Game()
{
InitializeComponent();
MessageBox.Show(Form2.playerStudent.Difficulty);
//this message box is empty
//other code...
}