Events Programming in C# Events CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.

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.
CS  C++ Function Pointers  C# Method References  Groundwork for Lambda Functions.
Recitation 11 November 11, Today’s Goals:  Automatic refresh  Learn and apply the Observer pattern.
Spring 2010ACS-3913 Ron McFadyen1 Weather Station Page 39+ In this application, weather station devices supply data to a weather data object. As the data.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Event Handling Events and Listeners Timers and Animation.
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++
Writing Object Oriented Software with C#. C# and OOP C# is designed for the.NET Framework  The.NET Framework is Object Oriented In C#  Your access to.
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
Event Notification Pattern Dr. Neal CIS 480. Event Notification Pattern When a class abc has something happening that class xyz needs to know about you.
Event-based Programming Roger Crawfis. Window-based programming Most modern desktop systems are window-based. Non-window based environment Window based.
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Programming Languages and Paradigms Object-Oriented Programming.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Understanding Events and Exceptions Lesson 3. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understand events and event handling Understand.
Data Binding to Controls Programming in C# Data Binding to Controls CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
CSC 298 Windows Forms.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
Programming in C# Observer Design Pattern
CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.
Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell”
Delegates Programming in C# Delegates CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
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.
COM148X1 Interactive Programming Lecture 7. Topics Today HCI Event Handling.
Delegates and Events Callback Functionality and Event-Driven Programming Svetlin Nakov Technical Trainer Software University
Object Oriented Software Development
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming.  Interface  Event Handling.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Introduction to Object-Oriented Programming Lesson 2.
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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Effective C# 50 Specific Way to Improve Your C# Item 22, 23.
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
Model View ViewModel Architecture. MVVM Architecture components.
Copyright © 2004, Keith D Swenson, All Rights Reserved. OASIS Asynchronous Service Access Protocol (ASAP) Tutorial Overview, OASIS ASAP TC May 4, 2004.
Arrays and Indexers Programming in C# Arrays and Indexers CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain.
Lecture 12 Implementation Issues with Polymorphism.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
CIS NET Applications1 Chapter 6 – Events. CIS NET Applications2 Objectives Delegate-based Events.NET event support Practical guides for managing.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 32 JavaBeans and Bean Events
Observer Pattern Context:
INF230 Basics in C# Programming
Delegates and Events Svetlin Nakov Telerik Corporation
Chapter 36 JavaBeans and Bean Events
Understand the Fundamentals of Classes
Delegates and Events 14: Delegates and Events
Programming in C# Properties
Windows Desktop Applications
C# Event Processing Model
6 Delegate and Lambda Expressions
Delegates & Events 1.
Programming in C# CHAPTER - 8
Fundaments of Game Design
CIS 199 Final Review.
Chapter 13: Handling Events
Events, Delegates, and Lambdas
Presentation transcript:

Events Programming in C# Events CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis

The event field An event field is just a special delegate instance. Usually exposed as a public field (acts more like a property). Restricts the delegate operations (to the public) to += and -=. Only the class (or its decendents) can fire the event. Only the class (or its descendents) can clear or reset the values (using =).

Published Properties public class SharedFloat { public delegate void NewValue(float value); public event NewValue ValueChanging; public event NewValue ValueChanged; private void Dummy(float value) {} public SharedFloat() { // This avoids the check for null. ValueChanging += Dummy; ValueChanged += Dummy; } private float myFloat; public float Value { get { return myFloat; } set { ValueChanging(value); myFloat = value; ValueChanged(myFloat); }

Example class Model { public event Notifier notifyViews; public void Change() {... notifyViews("Model"); } } class View { public View(Model m) { m.notifyViews += new Notifier(Update); } void Update(string sender) { Console.WriteLine(sender + " was changed"); } } class Test { static void Main() { Model model = new Model(); new View(model); new View(model);... model.Change(); }

Event Accessors Event subscription can be controlled/monitored within a class. Similar to properties – add accessor is called during +=, remove accessor is called during -= Both accessors need to be declared. public delegate void MyDelegate (); class A { private MyDelegate m_DelegateBehind; public event MyDelegate Event { add { m_DelegateBehind += value; } remove { m_DelegateBehind -= value; } } … }

Classes with events A class instance may publish several events. Each event member holds a collection of subscribers. As with delegates, the publishing object calls each registered listener in turn..

Naming Conventions in.NET The name of an event ends with ing if the notification occurs before the state change or actual event. The name of an event ends with ed if the notification occurs after the state change or actual event. Some typical event names form.Closing // has not closed yet, this can abort the close form.Closed // by this time the form is closed. msg.Sending // suggests you can intercept and modify // the message just before sending msg.Sent // telling you that it has already gone.

What does this buy us? Flexible, loose coupling Very clean separation of concerns Easily extensible: we can add new observers without having to modify the publisher. Modules can be “wired” to listen to one another as part of the startup logic, The only type coupling between the modules is determined by the type of the event delegate.

Examples in.NET Framework A Timer has a Tick event that you subscribe to. You can set the Interval between ticks. The EventLog component allows you to listen to EventWritten, which will alert you every time anything is written to your machine’s event log. The FileSystemWatcher component watches a directory structure. It uses a filter (e.g., “*.xml”) and exposes several events: Changed Created Renamed Deleted

The Event Pattern in.NET Delegates for event handling in.NET have the following signature: delegate void SomeEvent (object sender, MyEventArgs e); The return type is void. The first parameter is a reference to the class that contains the event (the publisher). The second parameter is used to pass (push) data to the subscribers. It’s type is derived from System.EventArgs. Using this pattern, I can use a method like the following for all.NET events (using contra-variance): private void MyEventHandler( object sender, EventArgs e) { // log the event … } This method takes any object as the first parameter and any object derived from EventsArgs as the second.

Example public delegate void KeyEventHandler (object sender, KeyEventArgs e); public class KeyEventArgs : EventArgs { public virtual bool Alt { get {...} } // true if Alt key was pressed public virtual bool Shift { get {...} } // true if Shift key was pressed public bool Control { get {...} } // true if Ctrl key was pressed public bool Handled { get{...} set {...} } // indicates if event was already handled public int KeyValue { get {...} } // the typed keyboard code... } class MyKeyListener { public MyKeyListener(...) { keySource.KeyDown += new KeyEventHandler(HandleKey);} void HandleKey (object sender, KeyEventArgs e) {...} }

Events Programming in C# Events CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis