I tried to use the code you provided but received
a few different errors that I am working through now.
I have now provided the property bodies.
In your example you show the code for the first assembly.
How would I implement the code in the second
assembly (the console app for instance)?
First, add a reference; the real question (that only you can
answer) is how does the caller get hold of the objects?
I've used a simple ctor below, but I guess there is more
to it than this.
Also - typically there would be a few more properties
in the {blah}Args class to represent what the subscriber
is likely to need; this could be done via params to
the On{blah} method, with corresponding params
in the {blah}Args constructor, with some properties
(typically readonly) on the {blah}Args class - e.g.
information about *what* was received.
Actually, you might not need the Connect/Disconnect classes
unless there is more to them than is shown; the base class
could me made non-abstract, and the trace command
could use:
Trace.WriteLine(GetType().Name + "::PacketOnReceive");
This would trace all subclasses correctly.
Marc
public class PacketEventArgs : EventArgs
{
private bool _result;
public bool Result {
get { return _result; }
set {_result = value;}
}
}
public abstract class PacketBase
{
public event EventHandler<PacketEventArgsPacketReceived;
protected virtual bool OnPacketReceived()
{
EventHandler<PacketEventArgshandler = PacketReceived;
if (handler == null) return false; // or true; whatever is a
good default
PacketEventArgs args = new PacketEventArgs();
handler(this, args);
return args.Result;
}
}
public class PacketConnect : PacketBase
{
protected override bool OnPacketReceived()
{ // just add trace info, and defer to base implementation
Trace.WriteLine("PacketConnect::PacketOnReceive");
return base.OnPacketReceived();
}
}
public class PacketDisconnect : PacketBase
{
protected override bool OnPacketReceived()
{ // just add trace info, and defer to base implementation
Trace.WriteLine("PacketDisconnect::PacketOnReceive ");
return base.OnPacketReceived();
}
}
public class Program
{
static void Main()
{
PacketDisconnect pd = new PacketDisconnect();
pd.PacketReceived += new
EventHandler<PacketEventArgs>(pd_PacketReceived);
}
static void pd_PacketReceived(object sender, PacketEventArgs e)
{
// only return tre on Tuesdays...
e.Result = (DateTime.Today.DayOfWeek == DayOfWeek.Tuesday);
}
}