Programming in C# CHAPTER - 8

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Advertisements

CS  C++ Function Pointers  C# Method References  Groundwork for Lambda Functions.
Microsoft Visual C#.NET: From Problem Analysis to Program Design1 Chapter 9 Programming Based on Events Microsoft Visual C#.NET: From Problem Analysis.
Programming Based on Events
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Delegates and Events Tom Roeder CS fa. Motivation – Function Pointers Treat functions as first-class objects eg. Scheme: (map myfunc somelist)
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 20 Microsoft’s Approach 3 – C# Rob Pooley
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
Object Oriented Programming, Interfaces, Callbacks Delegates and Events Dr. Mike Spann
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Events in C# Events in C#.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Events in C# MHA Delegates vs. Events Delegates can be used as events Example CountDownTimerEvent -> CountDownDelegate But have certain problems.
COMP 205 – Week 10 Chunbo Chu. Method Local variables The existence of a local variable is limited to the block in which it is created and the blocks.
Java2C# Antonio Cisternino Part II. Outline Array types Enum types Value types  differences from classes  boxing and unboxing Delegate types  Base.
CSC 298 Windows Forms.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.
G RAPHICAL U SER I NTERFACE C ONCEPTS : P ART 1 1 Outline Introduction Windows Forms Event-Handling Model - Basic Event Handling.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Effective C# 50 Specific Way to Improve Your C# Item 20, 21 Sephiroth.Wang2012/08/01.
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.
One of the most prevalent powerful programming languages.
Delegates and Events Callback Functionality and Event-Driven Programming Svetlin Nakov Technical Trainer Software University
Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
Callback Functionality and Event-Driven Programming
Neal Stublen Tonight’s Agenda  Indexers  Delegates and events  Operator overloading  Class inheritance  Q&A.
1.NETDelegates & eventsNOEA / PQC Delegates & events Observer pattern Delegates –Semantics –Cil – kode –Anvendelse Events.
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.
Chapter 9 Delegates and Events. Copyright 2006 Thomas P. Skinner2 Delegates Delegates are central to the.NET FCL A delegate is very similar to a pointer.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Events Programming in C# Events CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Appendix A: Windows Forms. 2 Overview Describe the structure of a Windows Forms application –application entry point –forms –components and controls Introduce.
1.NETDelegates & eventsNOEA / PQC 2007 Delegates & events Observer pattern Delegates –Semantics –Cil – kode –Anvendelse Events.
V 1.0 OE-NIK HP 1 Advanced Programming Delegates, Events.
Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
CIS NET Applications1 Chapter 6 – Events. CIS NET Applications2 Objectives Delegate-based Events.NET event support Practical guides for managing.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Chapter 9 Programming Based on Events
INF230 Basics in C# Programming
Delegates and Events Svetlin Nakov Telerik Corporation
Computing with C# and the .NET Framework
Delegates and Events 14: Delegates and Events
Chapter Eleven Handling Events.
Chapter 5: Programming with C#
Windows Desktop Applications
.NET and .NET Core: Languages, Cloud, Mobile and AI
C# Event Processing Model
6 Delegate and Lambda Expressions
Interface.
Chapter 10: Visibility Chapter 18 in Applying UML and Patterns Book.
Abstract Classes AKEEL AHMED.
Delegates & Events 1.
CIS16 Application Development and Programming using Visual Basic.net
DELEGATES AND EVENT MODELING
Chapter 13: Handling Events
Chapter 15 Event-Driven Programming and Animations Part 1
Object Oriented Programming
ITE “A” GROUP 2 ENCAPSULATION.
Events, Delegates, and Lambdas
Presentation transcript:

Programming in C# CHAPTER - 8

C# Delegates and Events A delegate in C# contains one or more method references: its invocation list. Invoking the delegate invokes the methods in its invocation list. Delegates are primarily used for event handling. delegate method invocation list

Basic delegate pattern in C# delegate void MyDelegate(int x); // declare a delegate type class Eater { static void EatAnInt(int x) {…} void ConsumeAnInt(int x) {…} // some methods for the invocation list static void Main() { Eater etr = new Eater(); MyDelegate d = new MyDelegate(EatAnInt) + new MyDelegate(etr.ConsumeAnInt); //create a delegate d(5); } //invoke it; both EatAnInt(x) and etr.ConsumeAnInt(x) will be invoked

Example from C# delegate // Declare a delegate type for processing a book: public delegate void ProcessBkDelegate(Book book); // Call a passed-in delegate on each book public void ProcessBooks(ProcessBkDelegate processBk) { foreach (Book b in list) { processBk(b); } } static void PrintTitle(Book b) {…} bookDB.ProcessBooks(new ProcessBkDelegate(PrintTitle)); PriceTotaller totaller = new PriceTotaller(); bookDB.ProcessBooks(new ProcessBkDelegate(totaller.AddBookToTotal));

Event Handling: Observer Pattern aSource aListener register eventInfo new notify(eventInfo) query

Event Handling Event source is a class-instance; listener is a method. Event source has a delegate member to store listeners. Keyword event is used to declare this member. Listener registers by adding itself to the delegate's invocation list. Event info is stored in an EventArgs object.

Example: C# Event Handling public delegate void FlashHandler(object sender,EventArgs e); class Thunderstorm { public event FlashHandler Lightning; } class Weatherman { static void CountFlashes(object sender, EventArgs e) {…} public static void Main() { Thunderstorm storm = new Thunderstorm(); storm.Lightning += new FlashHandler(CountFlashes); }

Semantics of the event keyword Differences between public event DelegateType FooEvent public DelegateType FooDelegate Event can be invoked only from inside the class: obj.FooEvent() is illegal! Even subclasses cannot directly invoke event! Outside the class, can only change invocation list: obj.FooEvent += new DelegateType(…); obj.FooEvent -= new DelegateType(…); Events can be declared in an interface.

Inheritance and C# events Since subclasses cannot invoke events, one often creates a method to invoke it: event ClickHandler Click; public OnClick() { if (Click!=null) Click(); } event ClickHandler Click; public virtual OnClick() { if (Click!=null) Click(); } This is also a good place to check that there are listeners: "if (Click != null) …"

.NET Framework Event Guidelines C# allows you to use any delegate type for event: delegate MyReturnType MyDelegateType(MyArgType); event MyDelegateType MyEvent; The .NET framework provides two system types: System.EventArgs (used for event info) System.EventHandler (delegate type) Signature of EventHandler: delegate void EventHandler(object sender,EventArgs e);

.NET Framework Event Guidelines In most cases, simply use EventHandler for your events: event EventHandler MyEvent; You pass in the source of the event as the sender, and you use Empty for the event args: MyEvent(this, EventArgs.Empty); If you need to pass more event info, you declare a subclass of EventArgs: class MyEventArgs:EventArgs {...} delegate void MyHandler(object src, MyEventArgs e); event MyHandler MyEvent;