Delegates and Events

Started by sukishan, Sep 04, 2009, 09:28 PM

previous topic - next topic
Go Down

sukishan

Delegates and Events
Because the Java programming language has no built-in support for delegates and events, the J# implementation provides a set of extensions for creating this capability. Delegates are tagged with a special comment /** @delgate */ before its definition and so are events (using /** @event */). Also, the class implementing the events needs to keep a reference to all the listeners assigned for the particular events by creating an ArrayList or a similar collection. After the listeners have been assigned, they are invoked using the Invoke method .

package hks;
import System.*;
import System.Collections.*;

/** @delegate */
public delegate void EventHandler();

public class Button
{
  ArrayList listeners = new ArrayList();
  public static void main()
  {
    Button button = new Button();
    button.add_OnClick(new EventHandler(Button_OnClick));
    button.Click();
  }
  /** @event */
  public void add_OnClick(EventHandler listener) {
    listeners.Add(listener);
  }
  /** @event */
  public void remove_OnClick(EventHandler listener) {
    listeners.Remove(listener);
  }
  public void Click()
    {
     Object [] olisteners = listeners.ToArray();
    for (int i = 0;i < olisteners.length ;i++)
       {
         ((EventHandler)(olisteners)).Invoke();
       }
    }
  public static void Button_OnClick()
  {
    Console.WriteLine("Button Clicked");
  }
}/** @delegate */
public delegate void EventHandler
A good beginning makes a good ending

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens | GinGly :: SMS Backup | Acumen :: Discussion Board | AshokPillar :: Hosting | CineBuzz :: Latest Cinema News | My Kids Diary :: Capture your kids magical moment
Copyright 2005 - 2017 :: IT Acumens :: All Rights Reserved.
ITAcumens Forum with 2 lakhs post running for 10 years - Powered by HostGator Dedicated Server