473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Event question

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
Nov 17 '05 #1
14 1767
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

Nov 17 '05 #2
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

"Johann Blake" <jo*********@yahoo.com> schrieb im Newsbeitrag
news:11********************@g43g2000cwa.googlegrou ps.com...
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

Nov 17 '05 #3
Rainer Queck wrote:
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].
What is it I am doing wrong?

Thanks for hints.
Rainer

Nov 17 '05 #4
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

Nov 17 '05 #5
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
Nov 17 '05 #6
Hi Johann,

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

Rainer
Nov 17 '05 #7
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

Nov 17 '05 #8
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
Nov 17 '05 #9
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

Nov 17 '05 #10
Sorry, I faild in my effort to explain what I want....

"Johann Blake" <jo*********@yahoo.com> schrieb im Newsbeitrag
news:11**********************@g43g2000cwa.googlegr oups.com...
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
Nov 17 '05 #11
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

Nov 17 '05 #12
In message <#M**************@TK2MSFTNGP09.phx.gbl>, Rainer Queck
<Ra**********@Qutronic.de> writes
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?
--
Steve Walker
Nov 17 '05 #13
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

"Steve Walker" <st***@otolith.demon.co.uk> schrieb im Newsbeitrag
news:DH**************@otolith.demon.co.uk...
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?
--
Steve Walker

Nov 17 '05 #14
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

"Johann Blake" <jo*********@yahoo.com> schrieb im Newsbeitrag
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Nov 17 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
2
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
2
by: JollyK | last post by:
Hi friends, This is my question.... From the Page Load event (or Page Init event), I would need to find which event had occurred that caused a PostBack, for example was it a event fired from...
0
by: Mike hofer | last post by:
I am studying up for my MCAD, and came across an interesting conundrum. According to my textbook, there are FOUR steps to publish an event: 1. Define a delegate type that specifies the prototype...
5
by: Mike Salter | last post by:
I created a page that reads a DB for questions and possible answers (usuallyYes/No). I create a panel for each group of questions, and add a panel for each question to the Group panel. To the...
6
by: Tim | last post by:
Hi, I have a module that acts as a publisher of events. The clients subscribe for the events using the '+=' operator. Instead, I would like the clients to call a method like "RegisterForXEvent"...
10
by: jack | last post by:
Hi guys, I am working on a project which requires an implementation of discrete event simulation in C using linked lists. I would greatly appreciate if someone could provide with some sources...
3
by: jm | last post by:
>From a C. Petzold book I have Programming Windows C#. It's a little old now, but anyway, it has on page 71 (shortened): form.Paint += new PaintEventHandler(MyPaintHandler); static void...
4
by: tshad | last post by:
I am just getting started with events and had a couple of questions on why they do what they do. If you have a textbox and you want to handle an event you can just do: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.