Advertisement

07.05.2008 at 09:10PM PDT, ID: 23541270
[x]
Attachment Details

Event and delegate

Asked by niceguy971 in C# Programming Language

Tags: C#

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().GetHashCode();
}
 
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
Attachments:
 
the code
 
 
Loading Advertisement...
 
[+][-]07.05.2008 at 10:29PM PDT, ID: 21939422

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C# Programming Language
Tags: C#
Sign Up Now!
Solution Provided By: GreenGhost
Participating Experts: 2
Solution Grade: A
 
 
[+][-]07.06.2008 at 12:00AM PDT, ID: 21939568

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.06.2008 at 03:48AM PDT, ID: 21939861

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.06.2008 at 07:00AM PDT, ID: 21940366

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.06.2008 at 05:57PM PDT, ID: 21942196

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.07.2008 at 04:55AM PDT, ID: 21944200

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628