Yes, alright, it is probably a stupid question but I can´t understand events. I mean that I understand what delegates are for, I can create avents and handle them but I don´t actually understand them. Let´s say I have the following code with event in the Form1.cs file:
private void btnSleep_Click(object sender, EventArgs e)
{
_currentPerson.Sleep();
}
private void lvPeople_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvPeople.SelectedItems.Count > 0)
{
_currentPerson = (Person)lvPeople.SelectedItems[0].Tag;
_currentPerson.FellAsleep += _currentPerson_FellAsleep;
}
}
void _currentPerson_FellAsleep(object sender, EventArgs e)
{
lvPeople.SelectedItems[0].BackColor = Color.Aqua;
}
In the Person class I have this:
public delegate void PersonEventsHandlers(Object sender, EventArgs e);
public event PersonEventsHandlers FellAsleep;
public void Sleep()
{
this._isSleeping = true;
FellAsleep(this, EventArgs.Empty);
}
So everything works fine, cool. But if I do this change and forget the Person events works anyways.
private void btnSleep_Click(object sender, EventArgs e)
{
_currentPerson.Sleep();
lvPeople.SelectedItems[0].BackColor = Color.Aqua;
}
So why should I use the Person events?!
Thank you.