SlideShare a Scribd company logo
1 of 41
Events In F#
Dr. Rajeshree Khande
Agenda
• Handling Events
• Creating Custom Events
• Processing Event Streams
• Implementing an Interface Event
Introduction
• Events allow objects to communicate with one
another through a kind of synchronous message
passing.
• Events allow classes to send and receive messages
between one another.
• Events can also be triggered by your applications or
by the operating system.
Introduction
• Events enable you to associate function calls with
user actions.
• Events can also be triggered by your applications or
by the operating system.
• In GUI, events are user actions like key press, clicks,
mouse movements, etc., or some occurrence like
system generated notifications.
• Applications need to respond to events when they
occur
Introduction
• Objects communicate with one another through
synchronous message passing.
• Events are attached to other functions; objects
register callback functions to an event, and these
callbacks are executed when (and if) the event is
triggered by some object.
The Event Class and Event Module
• The Control.Event<'T> Class helps in creating an
observable object or event.
• It has the following instance members to work with
the events −
Member Description
Publish Publishes an observation as a first
class value.
Trigger Triggers an observation using the
given parameters.
Creating Events
• Events are created and used through
the Event class.
• The Event constructor is used for creating an event.
• Steps to create event
1. Create Event
2. Expose event handler
3. Invoke Event Handler
4. add callbacks to event handlers.
Steps to create event
1. Create Event
2. Expose event handler
3. Invoke Event Handler
4. add callbacks to event handlers.
type Worker(name : string, shift : string) =
let mutable _name = name;
let mutable _shift = shift;
//creates event
let nameChanged = new Event<unit>()
let shiftChanged = new Event<unit>()
// exposed event handler
member this.NameChanged = nameChanged.Publish
member this.ShiftChanged = shiftChanged.Publish
//invokes event handler
member this.Name
with get() = _name
and set(value) = _name <- value
nameChanged.Trigger()
//invokes event handler
member this.Shift
with get() = _shift
and set(value) = _shift <- value
shiftChanged.Trigger()
//add callbacks to event handlers
let w1 = new Worker(“AAAA", "Evening")
w1.NameChanged.Add(fun () -> printfn "Worker changed name! New name:
%s" wk.Name)
wk.Name <- “AMIT“
w1.NameChanged.Add(fun () -> printfn "-- Another handler attached to
NameChanged!")
wk.Name <- “JAIN"
w1.ShiftChanged.Add(fun () -> printfn "Worker changed shift! New shift: %s"
w1.Shift)
w1.Shift <- "Morning“
w1.ShiftChanged.Add(fun () -> printfn "-- Another handler attached to
ShiftChanged!")
wk.Shift <- "Night"
Abstract Class
used as a base class of a hierarchy and represents
common functionality
can have implemented and unimplemented
members.
A class that inherits abstract class must provide
implementation of all abstract methods & Class
Abstract classes are used to achieve abstraction.
We cannot create an instance of an abstract class
because the class is not fully implemented.
Syntax
[<AbstractClass>]
type [ accessibility-modifier ] abstract-class-name =
[ inherit base-class-or-interface-name ]
[ abstract-member-declarations-and-member-
definitions ]
// Abstract member syntax.
abstract member member-name : type-signature
Example:1
Person
member x.Name
abstract Greet ()
Student
member x.StudentID
override x.Greet()
Teacher
member x.Expertise
override x.Greet()
[<AbstractClass>]
type Person(name) =
member x.Name = name
abstract Greet : unit -> unit
type Student(name, studentID : int) =
inherit Person(name)
member x.StudentID = studentID
override x.Greet() = printfn "Student %s" x.Name
type Teacher(name, expertise : string) =
inherit Person(name)
member x.Expertise = expertise
override x.Greet() = printfn "Teacher %s." x.Name
let st = new Student("Amit", 1234)
let tr = new Teacher("Apurva", "Java")
//Overriden Greet
st.Greet()
tr.Greet()
[<AbstractClass>]
type AbstractClass1() =
class
abstract member ShowClassName : unit -> unit
end
type DerivedClass1() =
class
inherit AbstractClass1()
override this.ShowClassName() = printf "This is derived class."
end
let a = new DerivedClass1()
a.ShowClassName()
Interface
• It provides pure abstraction.
• Abstraction is about hiding unwanted details while showing
most essential information.
• It is a collection of abstract methods.
• A Class which implements interface must provide definition
for its all methods.
Interface
Abstract Class
In an interface declaration the members are not
implemented.
The members are abstract, declared by
the abstract keyword.
We may provide a default implementation using
the default keyword.
We can implement interfaces either by using object
expressions or by using class types.
In class or object implementation, We need to
provide method bodies for abstract methods of the
interface..
Interface :Syntax
[ attributes ]
type interface-name =
[ interface ] [ inherit base-interface-name ...]
abstract member1 : [ argument-types1 -> ] return-type1
abstract member2 : [ argument-types2 -> ] return-type2
...
[ end ]
The keywords interface and end, which mark
the start and end of the definition, are optional
Interface :Example
type IPerson =
abstract Name : string
abstract Enter : unit -> unit
abstract Leave : unit -> unit
Calling Interface Methods
• Interface methods are called through the interface,
• not through the instance of the class or type
implementing interface.
• To call an interface method, you up cast to the
interface type by using the :> operator (upcast
operator).
Example :
(s :> IPerson).Enter()
(s :> IPerson).Leave()
Example:1
Person
abstract Name
abstract Enter ()
abstract Leave()
Student
member this.ID
member this.Name
member this.Enter()
member this.Leave()
Teacher
member this.ID
member this.Name
member this.Enter()
member this.Leave()
Implements
type Person =
abstract Name : string
abstract Enter : unit -> unit
abstract Leave : unit -> unit
type Student(name : string, id : int) =
member this.ID = id
interface Person with
member this.Name = name
member this.Enter() = printfn "Student entering premises!"
member this.Leave() = printfn "Student leaving premises!"
type Teacher(name : string, id : int) =
member this.ID = id
interface Person with
member this.Name = name
member this.Enter() = printfn "Teacher entering premises!“
member this.Leave() = printfn "Teacher leaving premises!“
let s = new Student("Amit", 1234)
let t = new Teacher("Rohit", 34)
(s :> Person).Enter()
(s :> Person).Leave()
(t :> Person).Enter()
(t :> Person).Leave()
Interface Inheritance
Interfaces can inherit from one or more base interfaces.
Interface1
abstract member doubleIt
Interface2
abstract member tripleIt
Interface3
inherit Interface1
inherit Interface2
abstract member printIt()
multiplierClass
member this.doubleIt(a)
member this.tripleIt(a)
member this.printIt(a)
Implements
type Interface2 =
abstract member tripleIt: int -> int
type Interface3 =
inherit Interface1
inherit Interface2
abstract member printIt: int -> string
type multiplierClass() =
interface Interface3 with
member this.doubleIt(a) = 2 * a
member this.tripleIt(a) = 3 * a
member this.printIt(a) = a.ToString()
let ml = multiplierClass()
printfn "%d" ((ml:>Interface3).doubleIt(5))
printfn "%d" ((ml:>Interface3).tripleIt(5))
printfn "%s" ((ml:>Interface3).printIt(5))
//using the subclasses
let p = new Person("Mohan")
let st = new Student("Aditi", 1234)
let tr = new Teacher("Sujit", "Java")
p.Greet()
st.Greet()
st.dispID()
tr.Greet()
tr.disp()
Hi, I'm Mohan
Hi, I'm Aditi
Student ID 1234
Hi, I'm Sujit
Expertise in Java
O/P
Overloading & Overriding
Overloading
Overriding
Overloading
Overriding
Differences………
Overloading Overriding
• Overloading occurs when
two or more methods in
one class have the same
method name but
different parameters
• Overriding means having
two methods with the
same method name and
parameters (i.e., method
signature).
• One of the methods is in the
parent class and the other is in
the child class.
Introduction
• Operator overloading allows programmers to provide new
behavior for the default operators
• We can redefine operator behavior according to your custom
need.
• Operators are functions with special names, enclosed in
brackets.
• We can overload all arithmetic operators.
• Operators must be defined as static.
• All unary operators must use ~ (tiled) operator to indicate
that this is unary operator.
Introduction
• We can redefine or overload most of the built-in operators
available in F#
//overloading + operator
static member (+) (a : Complex, b: Complex) =
Complex(a.x + b.x, a.y + b.y)
The above function implements the addition operator (+) for a user-
defined class Complex.
It adds the attributes of two objects and returns the resultant Complex
object.
Example
Implementing a complex class
with
+, and –
Operators overloaded
type Complex(x: float, y : float) =
member this.x = x
member this.y = y
//overloading + operator
static member (+) (a : Complex, b: Complex) =
Complex(a.x + b.x, a.y + b.y)
//overloading - operator
static member (-) (a : Complex, b: Complex) =
Complex(a.x - b.x, a.y - b.y)
// overriding the ToString method
override this.ToString() =
this.x.ToString() + " " + this.y.ToString()
//Creating two complex numbers
let c1 = Complex(7.0, 5.0)
let c2 = Complex(4.2, 3.1)
// addition and subtraction using the overloaded operators
let c3 = c1 + c2
let c4 = c1 - c2
//printing the complex numbers
printfn "%s" (c1.ToString())
printfn "%s" (c2.ToString())
printfn "%s" (c3.ToString())
printfn "%s" (c4.ToString())
Overriding in F#
• We can override a default behavior of a base class method
and implement it differently in the subclass or the derived
class.
• Methods in F# are not overridable by default.
• To override methods in a derived class, we have to declare a
method as overridable using keyword abstract and default
keyword
type Person(name) =
member x.Name = name
abstract Greet : unit -> unit
default x.Greet() = printfn "Hi, I'm %s" x.Name
type Person(name) =
member x.Name = name
abstract Greet : unit -> unit
default x.Greet() = printfn "Hi, I'm %s" x.Name
type Student(name, studentID : int) =
inherit Person(name)
member x.StudentID = studentID
override x.Greet() = printfn "Student %s" x.Name
member x.dispID()=printfn "Student ID %d" x.StudentID
type Teacher(name, expert : string) =
inherit Person(name)
member x.Expert=expert
override x.Greet() = printfn "Teacher %s." x.Name
member x.disp()= printfn "Experties in %s" x.Expert
//using the subclasses
let p = new Person("Mohan")
let st = new Student("Aditi", 1234)
let tr = new Teacher("Sujit", "Java")
//default Greet
p.Greet()
//Overriden Greet
st.Greet()
tr.Greet()

More Related Content

What's hot

C# Starter L06-Delegates, Event Handling and Extension Methods
C# Starter L06-Delegates, Event Handling and Extension MethodsC# Starter L06-Delegates, Event Handling and Extension Methods
C# Starter L06-Delegates, Event Handling and Extension MethodsMohammad Shaker
 
Flying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnightFlying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnightWiem Zine Elabidine
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4Vince Vo
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manualsameer farooq
 
Chap2 class,objects
Chap2 class,objectsChap2 class,objects
Chap2 class,objectsraksharao
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summaryEduardo Bergavera
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMMario Fusco
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywordsmanish kumar
 
Intro to functional programming - Confoo
Intro to functional programming - ConfooIntro to functional programming - Confoo
Intro to functional programming - Confoofelixtrepanier
 
Web Design & Development - Session 6
Web Design & Development - Session 6Web Design & Development - Session 6
Web Design & Development - Session 6Shahrzad Peyman
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAMaulik Borsaniya
 

What's hot (20)

Vb.net iv
Vb.net ivVb.net iv
Vb.net iv
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
C# Starter L06-Delegates, Event Handling and Extension Methods
C# Starter L06-Delegates, Event Handling and Extension MethodsC# Starter L06-Delegates, Event Handling and Extension Methods
C# Starter L06-Delegates, Event Handling and Extension Methods
 
Flying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnightFlying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnight
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Chap2 class,objects
Chap2 class,objectsChap2 class,objects
Chap2 class,objects
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
 
.NET F# Class constructor
.NET F# Class constructor.NET F# Class constructor
.NET F# Class constructor
 
CS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUALCS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUAL
 
Zio from Home
Zio from Home Zio from Home
Zio from Home
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
11. java methods
11. java methods11. java methods
11. java methods
 
Intro to functional programming - Confoo
Intro to functional programming - ConfooIntro to functional programming - Confoo
Intro to functional programming - Confoo
 
Web Design & Development - Session 6
Web Design & Development - Session 6Web Design & Development - Session 6
Web Design & Development - Session 6
 
Java Lab Manual
Java Lab ManualJava Lab Manual
Java Lab Manual
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 

Similar to .NET F# Events

Similar to .NET F# Events (20)

.NET F# Abstract class interface
.NET F# Abstract class interface.NET F# Abstract class interface
.NET F# Abstract class interface
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
functions
functionsfunctions
functions
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading
 
Interface
InterfaceInterface
Interface
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
DS Unit 6.ppt
DS Unit 6.pptDS Unit 6.ppt
DS Unit 6.ppt
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
Scala cheatsheet
Scala cheatsheetScala cheatsheet
Scala cheatsheet
 
Functions2.pptx
Functions2.pptxFunctions2.pptx
Functions2.pptx
 
Python_UNIT-I.pptx
Python_UNIT-I.pptxPython_UNIT-I.pptx
Python_UNIT-I.pptx
 
Wien15 java8
Wien15 java8Wien15 java8
Wien15 java8
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 

More from DrRajeshreeKhande

More from DrRajeshreeKhande (17)

Exception Handling in .NET F#
Exception Handling in .NET F#Exception Handling in .NET F#
Exception Handling in .NET F#
 
.Net F# Generic class
.Net F# Generic class.Net F# Generic class
.Net F# Generic class
 
F# Console class
F# Console classF# Console class
F# Console class
 
.net F# mutable dictionay
.net F# mutable dictionay.net F# mutable dictionay
.net F# mutable dictionay
 
F sharp lists & dictionary
F sharp   lists &  dictionaryF sharp   lists &  dictionary
F sharp lists & dictionary
 
F# array searching
F#  array searchingF#  array searching
F# array searching
 
Net (f#) array
Net (f#)  arrayNet (f#)  array
Net (f#) array
 
.Net (F # ) Records, lists
.Net (F # ) Records, lists.Net (F # ) Records, lists
.Net (F # ) Records, lists
 
MS Office for Beginners
MS Office for BeginnersMS Office for Beginners
MS Office for Beginners
 
Java Multi-threading programming
Java Multi-threading programmingJava Multi-threading programming
Java Multi-threading programming
 
Java String class
Java String classJava String class
Java String class
 
JAVA AWT components
JAVA AWT componentsJAVA AWT components
JAVA AWT components
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
 
Dr. Rajeshree Khande Java Interactive input
Dr. Rajeshree Khande Java Interactive inputDr. Rajeshree Khande Java Interactive input
Dr. Rajeshree Khande Java Interactive input
 
Dr. Rajeshree Khande :Intoduction to java
Dr. Rajeshree Khande :Intoduction to javaDr. Rajeshree Khande :Intoduction to java
Dr. Rajeshree Khande :Intoduction to java
 
Java Exceptions Handling
Java Exceptions Handling Java Exceptions Handling
Java Exceptions Handling
 
Dr. Rajeshree Khande : Java Basics
Dr. Rajeshree Khande  : Java BasicsDr. Rajeshree Khande  : Java Basics
Dr. Rajeshree Khande : Java Basics
 

Recently uploaded

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 

Recently uploaded (20)

YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 

.NET F# Events

  • 1. Events In F# Dr. Rajeshree Khande
  • 2. Agenda • Handling Events • Creating Custom Events • Processing Event Streams • Implementing an Interface Event
  • 3.
  • 4. Introduction • Events allow objects to communicate with one another through a kind of synchronous message passing. • Events allow classes to send and receive messages between one another. • Events can also be triggered by your applications or by the operating system.
  • 5. Introduction • Events enable you to associate function calls with user actions. • Events can also be triggered by your applications or by the operating system. • In GUI, events are user actions like key press, clicks, mouse movements, etc., or some occurrence like system generated notifications. • Applications need to respond to events when they occur
  • 6. Introduction • Objects communicate with one another through synchronous message passing. • Events are attached to other functions; objects register callback functions to an event, and these callbacks are executed when (and if) the event is triggered by some object.
  • 7. The Event Class and Event Module • The Control.Event<'T> Class helps in creating an observable object or event. • It has the following instance members to work with the events − Member Description Publish Publishes an observation as a first class value. Trigger Triggers an observation using the given parameters.
  • 8. Creating Events • Events are created and used through the Event class. • The Event constructor is used for creating an event. • Steps to create event 1. Create Event 2. Expose event handler 3. Invoke Event Handler 4. add callbacks to event handlers.
  • 9. Steps to create event 1. Create Event 2. Expose event handler 3. Invoke Event Handler 4. add callbacks to event handlers.
  • 10. type Worker(name : string, shift : string) = let mutable _name = name; let mutable _shift = shift; //creates event let nameChanged = new Event<unit>() let shiftChanged = new Event<unit>() // exposed event handler member this.NameChanged = nameChanged.Publish member this.ShiftChanged = shiftChanged.Publish
  • 11. //invokes event handler member this.Name with get() = _name and set(value) = _name <- value nameChanged.Trigger() //invokes event handler member this.Shift with get() = _shift and set(value) = _shift <- value shiftChanged.Trigger()
  • 12. //add callbacks to event handlers let w1 = new Worker(“AAAA", "Evening") w1.NameChanged.Add(fun () -> printfn "Worker changed name! New name: %s" wk.Name) wk.Name <- “AMIT“ w1.NameChanged.Add(fun () -> printfn "-- Another handler attached to NameChanged!") wk.Name <- “JAIN" w1.ShiftChanged.Add(fun () -> printfn "Worker changed shift! New shift: %s" w1.Shift) w1.Shift <- "Morning“ w1.ShiftChanged.Add(fun () -> printfn "-- Another handler attached to ShiftChanged!") wk.Shift <- "Night"
  • 13. Abstract Class used as a base class of a hierarchy and represents common functionality can have implemented and unimplemented members. A class that inherits abstract class must provide implementation of all abstract methods & Class Abstract classes are used to achieve abstraction. We cannot create an instance of an abstract class because the class is not fully implemented.
  • 14. Syntax [<AbstractClass>] type [ accessibility-modifier ] abstract-class-name = [ inherit base-class-or-interface-name ] [ abstract-member-declarations-and-member- definitions ] // Abstract member syntax. abstract member member-name : type-signature
  • 15. Example:1 Person member x.Name abstract Greet () Student member x.StudentID override x.Greet() Teacher member x.Expertise override x.Greet()
  • 16. [<AbstractClass>] type Person(name) = member x.Name = name abstract Greet : unit -> unit type Student(name, studentID : int) = inherit Person(name) member x.StudentID = studentID override x.Greet() = printfn "Student %s" x.Name type Teacher(name, expertise : string) = inherit Person(name) member x.Expertise = expertise override x.Greet() = printfn "Teacher %s." x.Name let st = new Student("Amit", 1234) let tr = new Teacher("Apurva", "Java") //Overriden Greet st.Greet() tr.Greet()
  • 17. [<AbstractClass>] type AbstractClass1() = class abstract member ShowClassName : unit -> unit end type DerivedClass1() = class inherit AbstractClass1() override this.ShowClassName() = printf "This is derived class." end let a = new DerivedClass1() a.ShowClassName()
  • 18. Interface • It provides pure abstraction. • Abstraction is about hiding unwanted details while showing most essential information. • It is a collection of abstract methods. • A Class which implements interface must provide definition for its all methods.
  • 20. Abstract Class In an interface declaration the members are not implemented. The members are abstract, declared by the abstract keyword. We may provide a default implementation using the default keyword. We can implement interfaces either by using object expressions or by using class types. In class or object implementation, We need to provide method bodies for abstract methods of the interface..
  • 21. Interface :Syntax [ attributes ] type interface-name = [ interface ] [ inherit base-interface-name ...] abstract member1 : [ argument-types1 -> ] return-type1 abstract member2 : [ argument-types2 -> ] return-type2 ... [ end ] The keywords interface and end, which mark the start and end of the definition, are optional
  • 22. Interface :Example type IPerson = abstract Name : string abstract Enter : unit -> unit abstract Leave : unit -> unit
  • 23. Calling Interface Methods • Interface methods are called through the interface, • not through the instance of the class or type implementing interface. • To call an interface method, you up cast to the interface type by using the :> operator (upcast operator). Example : (s :> IPerson).Enter() (s :> IPerson).Leave()
  • 24. Example:1 Person abstract Name abstract Enter () abstract Leave() Student member this.ID member this.Name member this.Enter() member this.Leave() Teacher member this.ID member this.Name member this.Enter() member this.Leave() Implements
  • 25. type Person = abstract Name : string abstract Enter : unit -> unit abstract Leave : unit -> unit type Student(name : string, id : int) = member this.ID = id interface Person with member this.Name = name member this.Enter() = printfn "Student entering premises!" member this.Leave() = printfn "Student leaving premises!"
  • 26. type Teacher(name : string, id : int) = member this.ID = id interface Person with member this.Name = name member this.Enter() = printfn "Teacher entering premises!“ member this.Leave() = printfn "Teacher leaving premises!“ let s = new Student("Amit", 1234) let t = new Teacher("Rohit", 34) (s :> Person).Enter() (s :> Person).Leave() (t :> Person).Enter() (t :> Person).Leave()
  • 27. Interface Inheritance Interfaces can inherit from one or more base interfaces. Interface1 abstract member doubleIt Interface2 abstract member tripleIt Interface3 inherit Interface1 inherit Interface2 abstract member printIt() multiplierClass member this.doubleIt(a) member this.tripleIt(a) member this.printIt(a) Implements
  • 28. type Interface2 = abstract member tripleIt: int -> int type Interface3 = inherit Interface1 inherit Interface2 abstract member printIt: int -> string type multiplierClass() = interface Interface3 with member this.doubleIt(a) = 2 * a member this.tripleIt(a) = 3 * a member this.printIt(a) = a.ToString() let ml = multiplierClass() printfn "%d" ((ml:>Interface3).doubleIt(5)) printfn "%d" ((ml:>Interface3).tripleIt(5)) printfn "%s" ((ml:>Interface3).printIt(5))
  • 29. //using the subclasses let p = new Person("Mohan") let st = new Student("Aditi", 1234) let tr = new Teacher("Sujit", "Java") p.Greet() st.Greet() st.dispID() tr.Greet() tr.disp() Hi, I'm Mohan Hi, I'm Aditi Student ID 1234 Hi, I'm Sujit Expertise in Java O/P
  • 33. Differences……… Overloading Overriding • Overloading occurs when two or more methods in one class have the same method name but different parameters • Overriding means having two methods with the same method name and parameters (i.e., method signature). • One of the methods is in the parent class and the other is in the child class.
  • 34. Introduction • Operator overloading allows programmers to provide new behavior for the default operators • We can redefine operator behavior according to your custom need. • Operators are functions with special names, enclosed in brackets. • We can overload all arithmetic operators. • Operators must be defined as static. • All unary operators must use ~ (tiled) operator to indicate that this is unary operator.
  • 35. Introduction • We can redefine or overload most of the built-in operators available in F# //overloading + operator static member (+) (a : Complex, b: Complex) = Complex(a.x + b.x, a.y + b.y) The above function implements the addition operator (+) for a user- defined class Complex. It adds the attributes of two objects and returns the resultant Complex object.
  • 36. Example Implementing a complex class with +, and – Operators overloaded
  • 37. type Complex(x: float, y : float) = member this.x = x member this.y = y //overloading + operator static member (+) (a : Complex, b: Complex) = Complex(a.x + b.x, a.y + b.y) //overloading - operator static member (-) (a : Complex, b: Complex) = Complex(a.x - b.x, a.y - b.y) // overriding the ToString method override this.ToString() = this.x.ToString() + " " + this.y.ToString()
  • 38. //Creating two complex numbers let c1 = Complex(7.0, 5.0) let c2 = Complex(4.2, 3.1) // addition and subtraction using the overloaded operators let c3 = c1 + c2 let c4 = c1 - c2 //printing the complex numbers printfn "%s" (c1.ToString()) printfn "%s" (c2.ToString()) printfn "%s" (c3.ToString()) printfn "%s" (c4.ToString())
  • 39. Overriding in F# • We can override a default behavior of a base class method and implement it differently in the subclass or the derived class. • Methods in F# are not overridable by default. • To override methods in a derived class, we have to declare a method as overridable using keyword abstract and default keyword type Person(name) = member x.Name = name abstract Greet : unit -> unit default x.Greet() = printfn "Hi, I'm %s" x.Name
  • 40. type Person(name) = member x.Name = name abstract Greet : unit -> unit default x.Greet() = printfn "Hi, I'm %s" x.Name type Student(name, studentID : int) = inherit Person(name) member x.StudentID = studentID override x.Greet() = printfn "Student %s" x.Name member x.dispID()=printfn "Student ID %d" x.StudentID type Teacher(name, expert : string) = inherit Person(name) member x.Expert=expert override x.Greet() = printfn "Teacher %s." x.Name member x.disp()= printfn "Experties in %s" x.Expert
  • 41. //using the subclasses let p = new Person("Mohan") let st = new Student("Aditi", 1234) let tr = new Teacher("Sujit", "Java") //default Greet p.Greet() //Overriden Greet st.Greet() tr.Greet()