I was testing the code below (iHadi made some changes ); the goal: changes to the properties would notify any interested listeners;i need to use the delegate and event). Why Show(object newValue) method is not working??? It does not display "Inside of Show() method"..
using System;
public delegate void EventDelegate(object newValue);
public class BankCustomer
{
private string _name;
private int _acctn;
public event EventDelegate PropertyChanged;
public String Name
{
get { return _name; }
set { _name = value; OnPropertyChanged(value); }
}
public int Acctn
{
get { return _acctn; }
set { _acctn = value; OnPropertyChanged(value); }
}
public BankCustomer(string n, int numb)
{
_name = n;
_acctn = numb;
}
public override bool Equals(object obj)
{
if ((object)obj == null)
return false;
if (!(obj is BankCustomer))
return false;
return (this._name == (obj as BankCustomer)._name) && (this._acctn == (obj as BankCustomer)._acctn);
}
public override int GetHashCode()
{
return this.ToString().GetHashCod
e();
}
public void DoIt(string newValue)
{
PropertyChanged += new EventDelegate(Show);
}
public void Show(object newValue)
{
Console.WriteLine("Inside of Show() method");
}
private void OnPropertyChanged(object newValue)
{
// Check that the event has some methods attached to it before raising it.
if (PropertyChanged != null)
PropertyChanged(newValue);
}
}
class TestCust2
{
public static void Main()
{
BankCustomer bob = new BankCustomer("Bob", 12345);
Console.WriteLine("Bob's name before we called SET" );
Console.WriteLine("Bob's name=" + bob.Name);
bob.Name="Bob Green"; //
Console.WriteLine("Bob's name After we called SET" );
Console.WriteLine("Bob's name=" + bob.Name);
}
}
Start Free Trial