472,784 Members | 692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Raising a base class event

Hi folks,

I have an object library where there are base classes that have a
declared event. I have hit the problem where the derived class cannot
raise the event. After some thought I had the idea of declaring a
protected method in the base class to raise the event, so that the
derived class can just call this method when it needs to raise this event.

Is this good form, or is there a better way.

eg

public class myBaseClass {

public delegate void ChangedDelegate();
public event ChangedDelegate OnChanged;

protected void RaiseChangedEvent() {
if (OnChanged != null)
OnChanged();
}
}

public class myDerivedClass : myBaseClass {

private string _someValue;

public string SomeValue {
get { return _someValue; }
set {
if (_someValue != value)
RaiseChangedEvent();
_someValue = value;
}
}
Nov 27 '05 #1
3 2361

"Simon" <si****************@nodomain.com> wrote in message
news:O1**************@tk2msftngp13.phx.gbl...
Hi folks,

I have an object library where there are base classes that have a declared
event. I have hit the problem where the derived class cannot raise the
event. After some thought I had the idea of declaring a protected method
in the base class to raise the event, so that the derived class can just
call this method when it needs to raise this event.

Is this good form, or is there a better way.


That is about the only way available. Its just fine
Nov 27 '05 #2
Your code is good, but there is one little detail that should be changed.
In the section:
set {
if (_someValue != value)
RaiseChangedEvent();
_someValue = value;
}
}

....you're rasing the changed event before the value is changed. The _someValue
member should be set first, and then ypu should call the RaiseChangedEvent
method. After all, the value hasn't changed until you assign a new value
to it.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/

Nov 27 '05 #3
Thanks for the feedback folks.

I take your point Anders. Thanks for that.

Simon

Anders Norås wrote:
Your code is good, but there is one little detail that should be
changed. In the section:
set {
if (_someValue != value)
RaiseChangedEvent();
_someValue = value;
}
}


...you're rasing the changed event before the value is changed. The
_someValue member should be set first, and then ypu should call the
RaiseChangedEvent method. After all, the value hasn't changed until you
assign a new value to it.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/

Nov 27 '05 #4

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

Similar topics

3
by: James Dunkerley | last post by:
Hi, I am trying to create a class which downloads a web page in the background and then raises an event in the original thread in which it was created. This class is going to be the basis of a...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
4
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written...
2
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
4
by: sloan | last post by:
I"m trying to figure out what concept I'm missing here, or if its not a good idea .. or what. Here is my example.. code is below. I have an employee class. It has an event that can be raised....
5
by: kmcmanus | last post by:
I have just started to write a few business classes that are largely made up of properties (getters and setters). For each setter I want to fire a changed event - each event will have a unique...
1
by: Asko Telinen | last post by:
Hi all. I ran into quite strange problem concerning the event raising inside FileSystemWatcher Delete event. First, i would like to describe a bit my environment. I have main GUI...
2
by: RSH | last post by:
Hi, I have been trying to follow a tutorial on raising an event from a user control and then handling it in the parent page. The article is a little vague so I believe I have the code in...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.