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 + "::PacketOnRece ive");
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<Pa cketEventArgsPa cketReceived;
protected virtual bool OnPacketReceive d()
{
EventHandler<Pa cketEventArgsha ndler = 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 OnPacketReceive d()
{ // just add trace info, and defer to base implementation
Trace.WriteLine ("PacketConnect ::PacketOnRecei ve");
return base.OnPacketRe ceived();
}
}
public class PacketDisconnec t : PacketBase
{
protected override bool OnPacketReceive d()
{ // just add trace info, and defer to base implementation
Trace.WriteLine ("PacketDisconn ect::PacketOnRe ceive");
return base.OnPacketRe ceived();
}
}
public class Program
{
static void Main()
{
PacketDisconnec t pd = new PacketDisconnec t();
pd.PacketReceiv ed += new
EventHandler<Pa cketEventArgs>( pd_PacketReceiv ed);
}
static void pd_PacketReceiv ed(object sender, PacketEventArgs e)
{
// only return tre on Tuesdays...
e.Result = (DateTime.Today .DayOfWeek == DayOfWeek.Tuesd ay);
}
}