Static events and OnEvent method.

B

BLUE

I have a static class, so I've declared all variables static and also the
event.

This is the OnEvent method:
{
if (EventName != null)
{
// Invokes the delegates
EventName(this, e);
}
}

MSDN says it should be declared in this way:
protected virtual void OnEventName(EventNameEventArgs e)

But in a static class I must declare it static: I cannot declare it virtual:
protected static void OnEventName(EventNameEventArgs e)

It's an error or I can declare it in this way?


Thanks, Luigi.
 
K

Kalpesh

Luigi,

Can you post the complete sample code, which I can run & see?
Also, what is the use of "this" in your example, if it is a static
class?

Kalpesh
 
J

Jon Skeet [C# MVP]

I have a static class, so I've declared all variables static and also the
event.

This is the OnEvent method:
{
if (EventName != null)
{
// Invokes the delegates
EventName(this, e);
}
}

MSDN says it should be declared in this way:
protected virtual void OnEventName(EventNameEventArgs e)

But in a static class I must declare it static: I cannot declare it virtual:
protected static void OnEventName(EventNameEventArgs e)

It's an error or I can declare it in this way?

It's not an error - it's just that OnEvent is sometimes overridden in
derived classes. That's what the "virtual" would be for - you're not
attempting that pattern, so it doesn't matter.
 
N

Nicholas Paldino [.NET/C# MVP]

Luigi,

The documentation is speaking about instance methods, specifically.

In your case, you want to send null for the sender (there is no this
reference in static methods).

Also, you don't have to worry about declaring the OnEventName method.
The reason that this pattern is suggested is so that derived classes can
easily override the behavior if need be. However, since your class is
static, you can't have a virtual static method, so this doesn't apply here.

Hope this helps.
 
A

Alex Meleta

Virtual methods are needed to be overridden. You cannot override the
static methods coz they are not related to instance (only to the type).
The Microsoft considers an OOP approach when the base functionality of
handler methods should be able to override and call event by
On<Something>.

So, in your case it's not an error it's other approach, but what means
"this" in the OnEvent method? )

WBR, Alex Meleta
Blog: http://devkids.blogspot.com

-----Original Message-----
From: BLUE [mailto:blue]
Posted At: Freitag, 27. April 2007 22:05
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Static events and OnEvent method.
Subject: Static events and OnEvent method.

I have a static class, so I've declared all variables static and also
the
event.

This is the OnEvent method:
{
if (EventName != null)
{
// Invokes the delegates
EventName(this, e);
}
}

MSDN says it should be declared in this way:
protected virtual void OnEventName(EventNameEventArgs e)

But in a static class I must declare it static: I cannot declare it
virtual:
protected static void OnEventName(EventNameEventArgs e)

It's an error or I can declare it in this way?


Thanks, Luigi.
 
B

BLUE

Thank you all!

I've found the solution you have proposed me only this morning:
http://msdn2.microsoft.com/en-us/library/ms229011(VS.80).aspx

"Do use a protected virtual method to raise each event. This is applicable
only to non-static events on unsealed classes, not to structures, sealed
classes, or static events."

"Do not pass null as the sender parameter when raising a non-static event.
On static events, the sender parameter should be null."
 

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

Top