Delegates To Process Object Informations on Event firing

Heres the case :

I have a number of bank account objects. On July 20 2005, the bank willl issue a gift certificate to all the bank member objects that have registered to hear this event occur.

I am trying to accomplish this through delegates. Could anyone provide me with a skeletal code frame which I can use to accomplish this task.

LVL 2
deersuperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

martie_11Commented:
In Globals.cs:
---------------------------------------------------------------
public delegate void IssueGiftCertificateHandler(object sender, IssueGiftCertificateArgs args);

public class IssueGiftCertificateEventArgs : EventArgs
{
     private int _GiftCertificateAmount = -1;
     
     public IssueGiftCertificateEventArgs(int GiftCertificateAmount)
     {
          _GiftCertificateAmount = GiftCertificateAmount;
     }

     public int getGiftCertificateAmount
     {
          get { return _GiftCertificateAmount; }    
     }
}

In BankAccount.cs
----------------------------------
public event Globals.IssueGiftCertificateHandler IssueGiftCertificate;

int _giftCertificateAmount;
private void TriggerGiftCertificate(int amount)  // i.e. have some other method call this when the conditions are met (i.e. July 20, 2005)
{
     _giftCertificateAmount = amount;
     OnIssueGiftCertificate();
}

// trigger event:
protected void OnIssueGiftCertificate()
{
     if(IssueGiftCertificate != null)
     {
          IssueGiftCertificate(this, new Globals.IssueGiftCertificateEventArgs(_giftCertificateAmount));
     }
}

In BankMember.cs
-----------------------------
protected BankAccount BankAccount1;

// code that handles the event
private void OnIssueGiftCertificate(object sender, Globals.IssueGiftCertificateEventArgs args)
{
     int giftCertificate = args.getGiftCertificateAmount;  
     // other logic
}

private void InitializeComponent()
{
     // Subscribe this object to the event:
     this.BankAccount1.OnIssueGiftCertificate += new Globals.IssueGiftCertificateHandler(this.OnIssueGiftCertificate);
}


Comments:  Put your delegate in a 'neutral' class.  i.e. I use Globals.cs for all my global, static, delegate, etc stuff.
Then, your BankAccount object triggers the event which your BankMember subscribes to and handles.

Hope that helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lavinderCommented:
Hi

I think you must have created a class named Bank, exposing an event named SpecialEvent

class Bank
{
 public event XyzDelegate SpecialEvent;

 public void processing()
 {
   //if date is 20 july 2005 then fire event
   Delegate[] subscribers;

   //if someone has subscribed to event
   if (this.SpecialEvent!=null)
   {
     subscribers=SpecialEvent.GetInvocationList(); //get all the subscribers to event

     for(int i=0;i<subscribers.Length;i+)
       ((XyzDelegate)subscriber[i])(nameOfGift);
   }
 }
}

For bankaccount classes, assuming all of them hold reference to Bank's instance.

class BankAccount
{
  void subscribeToEvent()
 {
   refToBank.SpecialEvent += new XyzDelegate(this.EventOccurred);
 }

 void EventOccurred(string nameOfGift)
 {
   //name of gift received
 }
}

Hope it helps

lsb
deersuperAuthor Commented:
thanks guys ! both of your examples helped me a lot !
Enjoy the points !
Did you ever feel delegates were a little unnecessary ! I do ! Any comments ?
martie_11Commented:
I love delegates and events! : )
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.