471,326 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 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 2321

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by James Dunkerley | last post: by
4 posts views Thread by Dave A | last post: by

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.