about events and delegate

T

Tony Johansson

Hello!

I have a snippet from a class called TemperatureMonitor below.
Now to my question as you can see I fire the machineOverheating event which
is of
type StopMachineDelegate in the Notify method.

Question 1: Would it be possible to fire the event machineOverheating from
another class insted and if yes
what requirement must this new class fulfil?
For example must this new class that will fire the machineOverheating event
perhaps be derived from this
TemperatureMonitor class.

I compare a little bit with the .NET framework class control and the class
button. Class control is the base class for class Button.
I assume that the event field declaration is done in the control class but
you can fire the event from a class that is different from the class where
you declared the event. So you fire the event from the Button class but the
event fiead declaration is done in the Control class.


class TemperatureMonitor
{
public delegate void StopMachineDelegate;
public event StopMachineDelegate machineOverheating;
....

private void Notify()
{
if ( machineOverheating != null)
{
machineOverheating ();
}
}
}

//Tony
 
K

kofisarfo

Tony,

To raise that machineOverheating event from another class you need
first to instantiate this class from that other class and then call
it.
For example:

TemperatureMonitor temperatureMonitor = new TemperatureMonitor();
temperatureMonitor.MachineOverheating ();

I think what you're talking about, using the Control and Button
classes as an example, is inheritence which is an altogether different
thing and in the case you've described it's usually better to choose
composition, as shown in the code above.

See:
..Net Reference - Class Design: Inheritance, Interface, or Composition?
http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=408

Kofi Sarfo
 
M

Marc Gravell

Re the Control example, this uses a standard pattern (shown below). In the
example of Button, it also exposes a public method (DoClick or something) to
allow external callers to invoke the corresponding code.

Marc

public class SomeBaseClass {
public event EventHandler SomeEvent;
protected virtual void OnSomeEvent() {
// arguably accept the arg object, but
// pointless for raw EventArgs
if(SomeEvent!=null) {
SomeEvent(this, EventArgs.Empty);
}
}
}

public class SomeDerivedClass : SomeBaseClass {
[some block of code]
// wants to invoke the event
OnSomeEvent();
[end some block]
}

public class SomeOtherDerivedClass : SomeBaseClass {
// wants to mutate the event
protected override void OnSomeEvent() {
// do something else first
base.OnSomeEvent();
// do something else after
}
}

public class SomeThirdDerivedClass : SomeBaseClass {
// wants to expose the event to the outside
public void DoSomeEvent() {
OnSomeEvent();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top