So I've been studying C# on my own for about 3 months now. I'm going through the Murach's C# 2015 textbook. I've reached Chapter 13, which exposes me to indexers, delegates, events, and operators. I've followed along easily enough up to this point, but these delegates and events, more so the delegates confuse me terribly. I've literally been stuck on this topic for the past two weeks, and am just as confused about these things as the day I started. I've participated in several hours worth of IRC dialog on freenode's ##csharp channel, I have read about 10 different articles, watched several hours worth of youtube videos, and I am completely baffled. If anyone can please, please help me understand what in the heck, this delegate is doing, and how the event is related to it, any information or explanation at all, would be incredibly appreciated. Here's the code..
This is the CustomerList class, which defines the fields, properties, methods, etc of a CustomerList object, to be instantiated at some point in the future. I understand that the delegate defines a "signature" which is its return type, and parameters, and that the only methods that can be used by a delegate must have an identical "signature". I understand that when the Add() and Remove() methods are called, they raise the Changed event, which is somehow associated with the delegate....
Here is the code for the frmCustomers class, which when instantiated, is an object. This particular object, frmCustmers instantiates a CustomerList object. In it's _load Event Handler, it subscribes to the Changed event, defined in CustomerList... please omg help me.. O_O
This is the CustomerList class, which defines the fields, properties, methods, etc of a CustomerList object, to be instantiated at some point in the future. I understand that the delegate defines a "signature" which is its return type, and parameters, and that the only methods that can be used by a delegate must have an identical "signature". I understand that when the Add() and Remove() methods are called, they raise the Changed event, which is somehow associated with the delegate....
C#:
using System.Collections.Generic;
namespace CustomerMaintenance
{
public class CustomerList
{
private List<Customer> customers;
public delegate void ChangeHandler(CustomerList customers);
public event ChangeHandler Changed;
public CustomerList()
{
customers = new List<Customer>();
}
public int Count => customers.Count;
public Customer this[int i]
{
get
{
return customers[i];
}
set
{
customers[i] = value;
Changed(this);
}
}
public void Fill() => customers = CustomerDB.GetCustomers();
public void Save() => CustomerDB.SaveCustomers(customers);
public void Add(Customer customer)
{
customers.Add(customer);
Changed(this);
}
public void Remove(Customer customer)
{
customers.Remove(customer);
Changed(this);
}
}
}
Here is the code for the frmCustomers class, which when instantiated, is an object. This particular object, frmCustmers instantiates a CustomerList object. In it's _load Event Handler, it subscribes to the Changed event, defined in CustomerList... please omg help me.. O_O
C#:
using System;
using System.Windows.Forms;
namespace CustomerMaintenance
{
public partial class frmCustomers : Form
{
public frmCustomers()
{
InitializeComponent();
}
private CustomerList customers = new CustomerList();
private void frmCustomers_Load(object sender, EventArgs e)
{
customers.Changed += customers =>
{
customers.Save();
FillCustomerListBox();
MessageBox.Show("The 'Changed' event has been raised.", "Event Raised");
};
customers.Fill();
FillCustomerListBox();
}
private void FillCustomerListBox()
{
lstCustomers.Items.Clear();
for (int i = 0; i < customers.Count; i++)
{
Customer c = customers[i];
lstCustomers.Items.Add(c.GetDisplayText());
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
frmAddCustomer addCustomerForm = new frmAddCustomer();
Customer customer = addCustomerForm.GetNewCustomer();
if (customer != null)
{
customers.Add(customer);
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
int i = lstCustomers.SelectedIndex;
if (i != -1)
{
Customer customer = (Customer)customers[i];
string message = "Are you sure you want to delete "
+ customer.FirstName + " " + customer.LastName + "?";
DialogResult button = MessageBox.Show(message, "Confirm Delete",
MessageBoxButtons.YesNo);
if (button == DialogResult.Yes)
{
customers.Remove(customer);
}
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}