CIS 330 -.NET Applications1 Chapter 6 – Events. CIS 330 -.NET Applications2 Objectives Delegate-based Events.NET event support Practical guides for managing.

Slides:



Advertisements
Similar presentations
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
CS  C++ Function Pointers  C# Method References  Groundwork for Lambda Functions.
CIS162AD Inheritance 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Constructors & Inheritance.
Road Map Introduction to object oriented programming. Classes
Delegates & Events Observer and Strategy Patterns Game Design Experience Professor Jim Whitehead January 30, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
Software Testing and Quality Assurance
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
Encapsulation CMSC 202. Types of Programmers Class programmers – Developers of new classes – Goal: Expose the minimum interface necessary to use a new.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Understanding Events and Exceptions Lesson 3. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understand events and event handling Understand.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.
Delegates Programming in C# Delegates CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
CCA Event Specification Proposal
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.
Delegates and Events Callback Functionality and Event-Driven Programming Svetlin Nakov Technical Trainer Software University
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
IBM TSpaces Lab 3 Transactions Event Registration.
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.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CSC 205 Java Programming II Defining & Implementing Classes.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
OOP with Objective-C Categories, Protocols and Declared Properties.
Copyright © 2007 InSTech Joint Laboratory All rights reserved. 1 Consideration on Persistence of WiseMan FuDan-Hitachi InSTech (Innovative Software Technology)
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
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.
C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Events Programming in C# Events CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
CLASS AND OBJECTS Valerie Chu, Ph.D. LeMoyne-Owen College 1.
1 Introduction to Object Oriented Programming Chapter 10.
1.NETDelegates & eventsNOEA / PQC 2007 Delegates & events Observer pattern Delegates –Semantics –Cil – kode –Anvendelse Events.
Reflections CSC207 – Software Design. Background Turing's great insight: programs are just another kind of data. Source code is text that is interpreted.
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 8 – Multithreading and Concurrency Management.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
I2rs Requirements for NETCONF IETF 93. Requirement Documents
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Chapter 32 JavaBeans and Bean Events
Chapter 2 Objects and Classes
Programming Right from the Start with Visual Basic .NET 1/e
C# for C++ Programmers 1.
Delegates and Events Svetlin Nakov Telerik Corporation
Computing with C# and the .NET Framework
Chapter 36 JavaBeans and Bean Events
Delegates and Events 14: Delegates and Events
Chapter Eleven Handling Events.
Chapter 5: Programming with C#
C# Event Processing Model
6 Delegate and Lambda Expressions
Delegates & Events 1.
Lesson 7. Events, Delegates, Generics.
CIS16 Application Development and Programming using Visual Basic.net
Programming in C# CHAPTER - 8
CIS16 Application Development and Programming using Visual Basic.net
5. 3 Coding with Denotations
Information Hiding and Encapsulation Section 4.2
C# COM Interoperability
Events, Delegates, and Lambdas
Presentation transcript:

CIS NET Applications1 Chapter 6 – Events

CIS NET Applications2 Objectives Delegate-based Events.NET event support Practical guides for managing events

CIS NET Applications3 Delegate-Based Events Source or Publisher: an object publishing the event Sink or Subscriber: any party interested in the event Firing: publishing an event Delegate: type-safe method reference

CIS NET Applications4 Delegate-Based Events (cont) Public delegate void NumberChangedEventHandler(int number); Public class MyPublisher { public NumberChangedEventHandler NumberChanged; /* other methods and members */ } Public class MySubscriber public void OnNumberChanged(int number) { string message = “New value is “ + number; MessageBox.Show(message,”MySubscriber”); }} MyPublisher publisher = new MyPublisher(); MySubscriber subscriber1 = new MySubscriber(); //Adding subscriptions Publisher.NumberChanged += new NumberChangedEventHandler (subscriber1.OnNumberChanged); //Firing the event Publisher.NumberChanged(3); //Removing subscriptions Publisher.NumberChanged -= subscriber1.OnNumberChanged; delegate inference Must have Same signature

CIS NET Applications5 Delegate-Based Events (cont) Potential problem with exposing the delegate as a public member Fix: define delegates as an event which only allows the publisher class to fire the event Public delegate void NumberChangedEventHandler(int number); Public class MyPublisher { public event NumberChangedEventHandler NumberChanged; public void FireEvent(int number) { NumberChanged(number) } /* other methods and members */ }

CIS NET Applications6.NET Loosely Coupled Events Can’t subscribe to type of event – only to publisher objects Events can’t be filtered Subscriber must be able to “see” Publisher – i.e., both must be running All “connections” must be done programmically

CIS NET Applications7 Working with.NET Events Defining Delegate Signatures –Target methods should have void return types –Signature should contain publisher’s identity –Use argument container like EventArgs.NET provides the EventHandler delegate: public delegate void EventHandler(object sender, EventArgs eventArgs); Publish events defensively – use try – catch blocks EventsHelper class – can be used to catch all exceptions while publishing to all delegates

CIS NET Applications8 Working with.NET Events (cont) Event Accessors: encapsulates the event member variable Managing large numbers of events: use.NET’s EventHandlerList class –Linear list that stores key/value pairs (event obj, delegate instance) Recommend writing Sink Interfaces –Define interface containing set of events –Subscriber implements interface –Publisher provides two methods, Subscribe() and Unsubscribe(); each uses two parameters, the interface and a way to endicate which events to subscribe to (author shows a way to do this using flags)

CIS NET Applications9 Summary Delegate-based Events.NET event support Practical guides for managing events