473,401 Members | 2,139 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,401 software developers and data experts.

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.
Apr 27 '07 #1
5 5882
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

On Apr 27, 2:04 pm, "BLUE" <bluewrote:
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.

Apr 27 '07 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"BLUE" <bluewrote in message
news:46***********************@reader5.news.tin.it ...
>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.


Apr 27 '07 #3
<"BLUE" <blue>wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 27 '07 #4
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.
Apr 27 '07 #5
Thank you all!

I've found the solution you have proposed me only this morning:
http://msdn2.microsoft.com/en-us/lib...11(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."
Apr 28 '07 #6

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

Similar topics

20
by: David | last post by:
I have a one-line script to add an onunload event handler to the body of the document. The script is as follows: document.getElementsByTagName("BODY").onunload=function s() {alert("s")} Now...
2
by: P. Gorecki | last post by:
Hi, I have a Sender and Reciever classes. I want the sender to send events to reciever but given a condition specified by reciever. For example, lets assume that sender is a keyboard and a...
2
by: COLIN JACK | last post by:
Hi All, I've got a situation where I'm implementing an interface (BaseInterface in example below) and I want to use explicity interface implementation of an event so that I can add type safety. ...
4
by: ThunderMusic | last post by:
Hi, I have a class let's say class1 that raises an event, let's say OnEvent. In another class, let's say class2, I have an array of Class1. I want to be able to trap the events fired by class1...
1
by: Asfar | last post by:
Here is my problem: In file form1.h I have the following code: #pragma once #include "Test.h" namespace AccessCheck { using namespace System; using namespace System::ComponentModel; using...
1
by: no spam | last post by:
Hi, I want to encapsulate code to change BUTTON elements on a webpage in one place in an HTC file. Here is the code in my HTC FIle: <PUBLIC:COMPONENT> <PUBLIC:ATTACH EVENT="onmouseup" ...
5
by: Richard Maher | last post by:
Hi, Here I mean "User" in the Programmer or Javascript sense. I merely wish to programmatically trigger an Event. It would be absolutely fantastic if there was a (Form level?) ONUSEREVENT() and...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
15
by: damiensawyer | last post by:
Hi, I am creating a class (from a base class) that needs to trigger events (code at bottom). I am instatiating the classes and wiring up the events as follows. clsDetermineConnection oDC =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.