こんにちは私は、人の生年月日が実際の日付よりも大きいときにイベントを起こそうとしています。C#でイベントを発生させる - ヘルプが必要
私はイベントで新しく、うまくいかないようです。コードの下にある
、
namespace LibClassLuchthaven
{
public class Person
{
public DateTime Birthdate { get; set; }
public string Firstname { get; set; }
public Gender Gender { get; set; }
public string Name { get; set; }
public event EventHandler BirthdateInFuture;
public Person()
{
this.Birthdate = DateTime.Now;
this.Firstname = string.Empty;
this.Gender = Gender.unknown;
this.Name = string.Empty;
}
public Person(DateTime birthdate, string firstname, Gender gender, string name)
{
this.Birthdate = birthdate;
this.Firstname = firstname;
this.Gender = gender;
this.Name = name;
}
public void OnBirthdateInFuture()
{
if (BirthdateInFuture!=null)
{
if (this.Birthdate > DateTime.Now)
{
BirthdateInFuture(this, EventArgs.Empty);
}
}
}
public override string ToString()
{
return this.Name + ", " + this.Firstname + " - " + this.Birthdate + " (+" + this.Gender + ")";
}
}
}
public partial class FormCrewManagement : Form
{
public Person person = new Person();
public FormCrewManagement()
{
InitializeComponent();
person.BirthdateInFuture += Person_BirthdateInFuture;
}
private void Person_BirthdateInFuture(object sender, EventArgs e)
{
MessageBox.Show("Birthdate is in the future");
}
"動作していない"と正確にはどういう意味ですか? –
あなたはどこでも 'OnBirthdateInFuture'を呼び出すようには思われません –
あなたはある時点でOnBirthdateInFutureメソッドを呼び出す必要があります。 – Seb