SlideShare a Scribd company logo
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

.NET F# Abstract class interface
.NET F# Abstract class interface.NET F# Abstract class interface
.NET F# Abstract class interfaceDrRajeshreeKhande
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdfprasnt1
 
.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloadingDrRajeshreeKhande
 
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 notesGOKULKANNANMMECLECTC
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1Vineeta Garg
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxtcsonline1222
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scalaehsoon
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#Dmitri Nesteruk
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 

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
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
 
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
 

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

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxDenish Jangid
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxJisc
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfbu07226
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptxJosvitaDsouza2
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsparmarsneha2
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxJisc
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismDeeptiGupta154
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasGeoBlogs
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxssuserbdd3e8
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...Jisc
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resourcesdimpy50
 

Recently uploaded (20)

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 

.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()