473,800 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Eve ntNameEventArgs e)

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

It's an error or I can declare it in this way?
Thanks, Luigi.
Apr 27 '07 #1
5 5903
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(Eve ntNameEventArgs e)

But in a static class I must declare it static: I cannot declare it virtual:
protected static void OnEventName(Eve ntNameEventArgs 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.co m

"BLUE" <bluewrote in message
news:46******** *************** @reader5.news.t in.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(Eve ntNameEventArgs e)

But in a static class I must declare it static: I cannot declare it
virtual:
protected static void OnEventName(Eve ntNameEventArgs 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(Eve ntNameEventArgs e)

But in a static class I must declare it static: I cannot declare it virtual:
protected static void OnEventName(Eve ntNameEventArgs 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.co m>
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.publi c.dotnet.langua ges.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(Eve ntNameEventArgs e)

But in a static class I must declare it static: I cannot declare it
virtual:
protected static void OnEventName(Eve ntNameEventArgs 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
12894
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 obviously, I put the alert("s") part in for debugging purposes, just to make sure the error wasn't in any code I was going to be running. This line works just fine in IE6 but in Firefox it doesn't. However, if I replace that line with the...
2
4850
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 reciever is a console. Lets assume that keyboard sends event every time key is pressed: Standard way of adding event handlers:
2
2515
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. To see what I mean look at the example below where the class implementing the interface actually wants the event to be for a more specific delegate. Now this seems to work but the code, to me is unnecessarily ugly. This leaves me wondering if...
4
1106
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 within class2 and know (if possible) the index of the class1 that fired the event. Is there a way? because withevents does not work. Here is some code you could rely on if you don't understand what I just explain :
1
1552
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 namespace System::Collections;
1
2731
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" ONEVENT="MouseUp()" /> <PUBLIC:ATTACH EVENT="onmouseover" ONEVENT="MouseOver()" />
5
4383
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 a setEvent() function but, in the absence of this, is it not possible to use DOM to change an object's properties resulting in a state-change event? I have tried with ONCHANGE() and that only seems to work if the "interactive" user changes the...
7
2345
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. Is that so?
15
6550
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 = new clsDetermineConnection(Request); oDC.LogMessage += new RunTraceBase.TraceArguments(LogMessagesFromEvents);
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10276
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9090
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7580
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.