Sei sulla pagina 1di 18

PCS4 Overview

Advanced programming fundamentals


Algorithms
PCS 4: Overview
Wee Subject Book
k
1 Delegates and events Nothing
2 RFID reader § 18.3.1
Sorting (selection sort)
3 Recursion § 7.15 and §
Binary Search algorithms 18.2
4 Passing arguments § 7.16
Pass by value / pass by
reference
ref and out parameters
5 Linked lists § 19.4
6 Binary trees § 19.7
7 Repetition
8 or 9 EXAM
PCS4

Week 1: Delegates and events


Delegate

4
Roughly speaking: a delegate stands in
for an actual method
1. Define the prototype (the heading) of the actual method the delegate
will stand for.
2. Then define a delegate.
3. Then tell the system for which actual method the delegate stands
for.

A delegate is a mysterious object that holds a reference to a method of


an object. That method fits to the prototype (has the same signature).

Calling the delegate means calling the actual method.

During runtime you may change the actual method the delegate stands
for.

Example: see next slide.

5
An example of using a delegate
public int CalcAddingAnswer public int CalcMultiplyingAnswer
(int a, int b) (int a, int b)
{ return a + b; } { return a * b; }

public delegate int CalculateAnswerHandler(int a, int b);


//Defines the prototype (signature) of the actual method

public CalculateAnswerHandler CalculateRightAnswer;


//Defines the delegate CalculateRightAnswer

CalculateRightAnswer = new CalculateAnswerHandler( CalcAddingAnswer );


//Assigns CalcAddingAnswer as the actual method to the delegate

a = CalculateRightAnswer(4,5);
//Now the actual method CalcAddingAnswer, with the values 4 and 5 for
its parameters, is performed. So the return-value is 9.

Demo: show it ! ! !
6
Possibilities with and limitations of
delegates
1. “Calling the delegate means calling the actual method”.
Who can do the calling? Everybody who can access it (depending
on the access-modifier).
2. “Tell the system for which actual method the delegate stands for”.
Suppose a delegate stands for no actual method. What happens
when you call the delegate now?
Answer: an exception.

Can we have delegates which have multiple actual methods?


Yes, all of them will be executed, but if they return something, you
only get the last return-value.

A former slide: A delegate is a mysterious object.

Now: A delegate has a list of mysterious objects.


Demo: show it ! ! !
7
Events

8
Events

public delegate int CalculateAnswerHandler(int a, int b);

public CalculateAnswerHandler ppp;


// ppp is a delegate

public event CalculateAnswerHandler qqq;


// qqq is an event

Events:
1. Who can do the calling? ONLY from inside this object (NOT
depending on the access-modifier).
2. Can you assign an actual method from outside the class to an
event? Depends on its access-modifier.
3. Can an event stand for 0, 1 or more actual methods?
Yes, the same as for a delegate.

9
Events
Objects should take care of themselves; just notify the others, who are
interested in you.

An example:

The software of a motor management system of a motorbike detects that


its speed is too high.
Notify those who are interested by raising an event.

The "listeners" (the actual methods) are performed


(could be a method of “Form1” to display on the bike’s dashboard;
or a method to insert a record in the incident-database of this bike,
or . . . ).

10
Example
public class Motorbike
{
private int speed;
private int maxSpeed;

public delegate void SpeedChangedHandler


(Motorbike sender, int extraInfo);

public event SpeedChangedHandler SpeedChanged;


public event SpeedChangedHandler SpeedLimitExceeded;

11
Example
public class Motorbike
{
private int speed;
private int maxSpeed;

public delegate void SpeedChangedHandler


(Motorbike sender, int extraInfo);

public event SpeedChangedHandler SpeedChanged;


public event SpeedChangedHandler SpeedLimitExceeded;

event, so only this


public, so you can attach object can call it.
actual methods to it.

12
Example
public class Motorbike
{

public void Accelerate(int inc)


{
int oldSpeed = this.speed;
this.speed += inc;

SpeedChanged(this, this.speed);

if (oldSpeed<=this.maxSpeed && this.speed>this.maxSpeed)


{
SpeedLimitExceeded(this, this.speed - this.maxSpeed);
}
}

13
Example
public class Motorbike
{

public void Accelerate(int inc)


{
int oldSpeed = this.speed; raising the event.
this.speed += inc;

SpeedChanged(this, this.speed); // be aware . . .(later)

if (oldSpeed<=this.maxSpeed && this.speed>this.maxSpeed)


{
SpeedLimitExceeded(this, this.speed - this.maxSpeed);
}
}

14
Example
In another class:

private void ShowInListbox(Motorbike m, int s)


{
this.listBox1.Items.Add(m.NameOfDriver + "...");
}

private Motorbike ValentinosBike;


ValentinosBike = new Motorbike("Valentino Rossi", 200);

ValentinosBike.SpeedLimitExceeded +=
new Motorbike.SpeedChangedHandler(this.ShowInListbox);

Now the method ShowInListbox


Demo: show it ! ! ! is an actual method of the
event SpeedLimitExceeded of
ValentinosBike.
15
A remark about events
Only raise them if they exists.

In the example:
SpeedChanged(this, this.speed); // wrong

if (SpeedChanged!=null)
{
SpeedChanged(this, this.speed);
}
// correct

16
Another remark about events
There is a difference between
(a) ValentinosBike.SpeedLimitExceeded +=
new Motorbike.SpeedChangedHandler(this.ShowInListbox);
and
(b) ValentinosBike.SpeedLimitExceeded =
new Motorbike.SpeedChangedHandler(this.ShowInListbox);
(a):
If the event does not exist, it is created and the delegate-object is added
to it. If the event already exists, the delegate-object is added to it.

(b):
NOT possible for an event; it is possible for a delegate.
If the delegate does not exist, it is created and the delegate-object is
added to it. If the delegate already exists, all already attached delegate-
objects are removed and the new delegate-object is added to it.

17
Here's is one more remark about events

Removing:
ValentinosBike.SpeedLimitExceeded -=
new Motorbike.SpeedChangedHandler(this.ShowInListbox);

If the event has a delegate-object for this.ShowInListbox,


then the last added delegate-object for it is removed.
(strange syntax: yes I know!)

18

Potrebbero piacerti anche