DELEGATES AND EVENT MODELING

Slides:



Advertisements
Similar presentations
Module 12: Operators, Delegates, and Events. Overview Introduction to Operators Operator Overloading Creating and Using Delegates Defining and Using Events.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Programming Based on Events
Button click handlers Maarten Pennings. Introduction An activity has one or more views – view is also known as widget or control – examples include Button,
Object-Oriented Programming with Java Lecture 2 The Java Event Model.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
GUI Basics and Event- Driven Programming Feb 7, 2000.
Object Oriented Programming, Interfaces, Callbacks Delegates and Events Dr. Mike Spann
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Understanding Events and Exceptions Lesson 3. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understand events and event handling Understand.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Java2C# Antonio Cisternino Part II. Outline Array types Enum types Value types  differences from classes  boxing and unboxing Delegate types  Base.
Web Services Week 2 Aims: Getting started with creating simple C# applications within Visual Studio.NET Objectives: –An introduction to the syntax of C#.NET.
CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell”
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Introduction to Java Beans CIS 421 Web-based Java Programming.
Object Oriented Programming.  Interface  Event Handling.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Introduction to Object-Oriented Programming Lesson 2.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
MiniDraw Introducing a Framework... and a few patterns.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Chapter 32 JavaBeans and Bean Events
CSC 205 Programming II Lecture 5 AWT - I.
Chapter 15 Event-Driven Programming and Animations
INF230 Basics in C# Programming
Polymorphism.
Chapter 36 JavaBeans and Bean Events
Web Design & Development Lecture 11
Exception and Event Handling
Introduction to JavaScript Events
Delegates and Events 14: Delegates and Events
Chapter Eleven Handling Events.
Introduction to Events
Multithreading in Java
GUI Event Handling Nithya Raman.
C# Event Processing Model
6 Delegate and Lambda Expressions
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Delegates & Events 1.
Lesson 7. Events, Delegates, Generics.
Android Topics Asynchronous Callsbacks
CS2011 Introduction to Programming I Methods (II)
Programming in C# CHAPTER - 8
Introduction to AppInventor
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Functions.
Chapter 13: Handling Events
Chapter 15 Event-Driven Programming and Animations Part 1
Object Oriented Programming
Java Basics Exception Handling.
Creating and Using Classes
Chengyu Sun California State University, Los Angeles
Events, Delegates, and Lambdas
Presentation transcript:

DELEGATES AND EVENT MODELING Created by : Asst. Prof. Ashish Shah DELEGATES AND EVENT MODELING

DELEGATES INTRODUCTION Created by : Asst. Prof. Ashish Shah DELEGATES INTRODUCTION DELEGATES ALLOWS THE PROGRAMMER TO ENCAPSULATE A REF. TO A METHOD INSIDE A DELEGATE OBJECT. DELEGATE OBJECT CAN THEN BE PASSSED TO CODE WHICH CAN CALL THE REFERENCED METHOD, WITHOUT HAVING TO KNOW AT COMPILE TIME WHICH METHOD WILL BE INVOKED.

PROPERTIES OF DELEGATES Created by : Asst. Prof. Ashish Shah PROPERTIES OF DELEGATES IT IS A TYPE SAFE. IT ALLOWS METHODS TO BE PASSED AS PARAMETERS. IT CAN BE USED TO DEFINE CALLBACK METHODS. IT CAN BE USED TO CHAIN TOGETHER ie. MULTIPLE METHODS CAN BE CALLED ON A SINGLE EVENT.

WHERE DELEGATES ARE USED? Created by : Asst. Prof. Ashish Shah WHERE DELEGATES ARE USED? USED WHERE THE METHOD NEEDS TO BE INVOKED BY THE RUNTIME WHEN THE EVENT OCCURS, HENCE THIS METHOD IS PASSED AS A PARAMETER TO A DELEGATE. TO START A NEW THREAD FOR A METHOD WHICH IS SUPPOSED TO BE PASSED TO DELEGATE. IT IS ALSO USED AS GENERIC CLASS LIBRARIES WHICH HAVE GENERIC FUNCTIONALITY DEFINED. THIS CAN BE DONE BY PASSING THE USER DEFINED FUNCTIONS TO DELEGATES.

using System; public delegate void deleint(int x); public delegate void deleString(string x); class eg_of_dele { public void inc(int n) c.w.(“after increment : “ + ++n); } class eg_of_dele2 public void strshow(string s) c.w.(“hello : “ + s); class del_eg_main psvm(string []args) eg_of_dele d1=new eg_of_dele(); deleint dint=new deleint(d1.inc); dint(5); eg_of_dele d2=new eg_of_dele(); deleint dstr=new deleString(d2.strshow); dint(“jmpc”); Created by : Asst. Prof. Ashish Shah

Events Event can be visualized as follows. Created by : Asst. Prof. Ashish Shah Event can be visualized as follows. Object notifies everyone that an event has occured An event occurs in an object Some one acts upon it Someone listens for that event

Event mechanism Declare a delegate Fire the events Created by : Asst. Prof. Ashish Shah Following figure illustrates the event raising mechanism. Event models basic idea is “publisher and subscribers”. Declare a delegate Declare an event based on delegate Fire the events Application2 (subscriber) Application1 (subscriber) Subscriber event Subscriber event Fire the event handler Fire the event handler Event handler Event handler

Example of event handling using delegates using System; public delegate void eventcheck(); class event_impl { public event eventcheck click; pubilc void onClick() if(click != null) click(); } class event_example public static void handle() c.w.(“click event occurs”); psvm(string [] args) event_impl ei=new event_impl(); ei.click += new eventcheck(handle); ei.onClick(); o/p: click event occurs Created by : Asst. Prof. Ashish Shah Example of event handling using delegates