Event question

  • Thread starter Thread starter Rainer Queck
  • Start date Start date
R

Rainer Queck

Hi,

in a base class I have declared the following event handler:

public abstract class MyBaseClass : :MarshalByRefObject
{
.....
public event EventHandler OnMsgSent;
....
}


In a derived class I try to call this event in the following way:

public override void MsgSent()
{
if(OnMsgSent != null)
{
GbMessageEventArgs e = new GbMessageEventArgs(this);
e.InfoString = "Message was successfully sent!";
OnMsgSent(this,e);
}
}

but when I trie to compile this, I get the following error message:
"The event ,<my namespace>.<base class>.OnMsgSent' can only appear on the
left hand side of += ...."

What is it I am doing wrong?

Thanks for hints.
Rainer
 
I don't believe that you can override an event. Since OnMsgSent is
defined as an even using:

public event EventHandler OnMsgSent;

it must always be defined in this manner. You cannot create a *method*
called OnMsgSent. A method and an event are two different things.

What exactly do you want to accomplish?

Best Regards
Johann Blake
 
Hi Johann,

thanks for answering!

I (at leas I am not aware of...) don't override the event.
In the base class (which is abstract) I declared the abstract method:
public abstract void MsgSent();
My intention is that every derived class must implement this mehtod.
The purpose of this method is to call all assigned delegates - OnMsgSent -
declared as event handles as previously shown.

Regards
Rainer
 
Rainer said:
Hi,

in a base class I have declared the following event handler:

public abstract class MyBaseClass : :MarshalByRefObject
{
....
public event EventHandler OnMsgSent;
...
}


In a derived class I try to call this event in the following way:

public override void MsgSent()
{
if(OnMsgSent != null)
{
GbMessageEventArgs e = new GbMessageEventArgs(this);
e.InfoString = "Message was successfully sent!";
OnMsgSent(this,e);
}
}

but when I trie to compile this, I get the following error message:
"The event ,<my namespace>.<base class>.OnMsgSent' can only appear on the
left hand side of += ...."

[I have doubt that this error appears where you register the event, can
you show us how you register the event too].
 
If someone wants to override your event, they just create the same
event in their derived class as you did using the same syntax.

Johann Blake
 
Hi Jianwei,
[I have doubt that this error appears where you register the event, can
you show us how you register the event too].

at the moment the event is not (yet) registered. I am preparing some classes
for udp message handling.
I want these classes to later on call the assigned delegates.

Rainer
 
Hi Johann,

so it is not possible to "outfit" a base class with events and then inherrit
from it?

Rainer
 
Hi Rainer,

You will always inherit events from a base class if they are public.

I am kind of confused on what you want to accomplish. Do you want to
override the event or do you simply want the derived class to be
informed when the base class raises the event?

Johann
 
Hi Johann,
I am kind of confused on what you want to accomplish. Do you want to
override the event or do you simply want the derived class to be
informed when the base class raises the event?
Sorry for confusing you.

My idea is to have a abstrac "generic message class" which at the momen
looks like :

public abstract class GenericMessage: MarshalByRefObject
{
public abstract void MsgRepeated();
public abstract void MsgSent();
public abstract void MsgTransmissionError();
public virtual event EventHandler OnMsgSent;
public virtual event EventHandler OnRepeat;
public virtual event EventHandler OnTransmissionError;
}

Then I want to drive some specific message classes from this class like for
example:

public class UdpTelegram1 : GenericMessage
{
.....
}
public class UdpTelegram2 : GenericMessage
{
.....
}
..... and so on

These casses shell represent udp messages (streams), holding information in
a format used in our company.

To all this there will be a "communictaion" class, to process the messages
to be sent and received, whithout knowing the specific type of a message
(polymorphism).
If a message was sent, it would just call the GenericMessage.MsgSent() in
order to inform the sender about the success.

In order that this can work, the derived messag classes need to implement
the correspoinding methods for the notification like for example:

public override void MsgSent()
{
if(OnMsgSent != null)
{
GbMessageEventArgs e = new GbMessageEventArgs(this);
e.InfoString = "Message was successfully sent!";
OnMsgSent(this,e);
}
}

I hope I was able to explain my intentions without confusing you more.

The problem seems to be, that the compiler won't let me use the events
declared in the base class in the derived classes?
Is it forbidden in C# to inherrit events?

Rainer
 
So all you really want is your derived class to be informed by the base
class when the event occurs. Just have your derived class subscribe to
the event:

public class UdpTelegram1 : GenericMessage
{
this.MsgSent += new YourDelegate(SomeEvent);

// Gets fired when the base class raises the event.
public void SomeEvent()
{

}

}

Best Regards
Johann Blake
 
Sorry, I faild in my effort to explain what I want....

Johann Blake said:
So all you really want is your derived class to be informed by the base
class when the event occurs. Just have your derived class subscribe to
the event:
No, this is not what i want.

what I want is:

Some application:
....
UdpTelegram1 aMsg = new UdpTelegram1();
aMsg.OnMsgSent += new EventHandler(Application.OnMessageSent)
...... prepare the telegram and then....
CommunicationProcess.SendMessage(aMsg);
.....
The communictaion process expects a "generic" message of the type
GenericMessage!

After the communication process has successfully sent the telegram it would
then inform the application by:
aMsg.MsgSent();

I hope this makes it a little more understandable....?

Rainer
 
So what's the problem? Just pass the variable referencing your event
like you are showing. Your SendMessage method only needs to have an
EventHandler parameter. That's all.

Johann
 
Rainer Queck said:
but when I trie to compile this, I get the following error message:
"The event ,<my namespace>.<base class>.OnMsgSent' can only appear on the
left hand side of += ...."

What is it I am doing wrong?

You are trying to raise the event from a derived class, and that isn't
allowed. Add a protected method to the base class which takes the
parameters you need and fires the event.

You want this to behave something like this, I presume:

abstract class Alarm
{
public event OnAlarmHandler OnAlarm;
public delegate void OnAlarmHandler();
protected void RaiseTheAlarm()
{
if(this.OnAlarm != null)
{
this.OnAlarm();
}
}
}
class SmokeAlarm:Alarm
{
void Check()
{
if(this.SmellsSmoke)
{
this.RaiseTheAlarm();
}
}
}

class FireMan
{
ArrayList alarms;
void ListenForAlarm(Alarm alarm)
{
alarm.OnAlarm+=new OnAlarmHandler(this.PutTheFireOut);
this.alarms.Add(alarm);
}
void PutTheFireOut()
{
//pour on water
}
}

FireMan fireMan = new FireMan();
fireMan.ListenForAlarm(new SmokeAlarm());
fireMan.ListenForAlarm(new FireAlarm());

etc?
 
Hi Steve,

thanks a lot for your help.
What you described, is exactly what I try to achieve.
What I was not aware of is, that one can't raise events from base classes
even though they are declared public.
In Delphi this is possible and this is where my problem had its origin ;-)

Regards
Rainer
 
Hi Johann,

thanks for your efforts.
Steve (a little down in this thread) had the answer that brought my on my
way again ;-)

Regards
Rainer
 

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

Back
Top