468,242 Members | 1,687 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

implement function in separate assembly.

I'm running into trouble and I hope someone can help.

I have two assemblies. The first defines a base class and a series of
distinct classes based on the base class. Something similar too:

// BASE CLASS
public class PacketBase
{
public virtual bool PacketOnReceive()
{
Trace.WriteLine("PacketBase::PacketOnReceive");
return true;
}
}

// DISTINCT CLASSES
public class PacketConnect : PacketBase
{
public override bool PacketOnReceive()
{
Trace.WriteLine("PacketConnect::PacketOnReceive");
return true;
}
}

public class PacketDisconnect : PacketBase
{
public override bool PacketOnReceive()
{
Trace.WriteLine("PacketDisconnect::PacketOnReceive ");
return true;
}
}

What I want to do is in my second assembly, which is a c# console
application, is define the PacketOnReceive for the distinct classes.
This way I can customize what happens when the function PacketOnReceive
is called. Then I could could create any number of applications based
on the first assembly that would have custom code.

Any help is appreciated.

Aug 8 '06 #1
7 1500
Generally you have 2 options, then; inheritance or events. There is no
reason that the console app can't itself subclass PacketConnect and
PacketDisconnect to provide additional features; all you would need is a
factory mechanism so that the right concrete types are used downstream.
Alternatively, exposing events on the classes (or even just the base class)
would allow any number of callers to provide their own implementations
without needing to subclass.

To me, it sounds like you want the latter (although the former may be a
little faster; not a lot in in in 2.0 though) - so you could (for instance):

public class PacketEventArgs : EventArgs {
public bool Result {get; set;} // EFR, as is ctor
}
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;
}
}

Now callers to any concrete PacketBase implementation can subscribe to
PacketReceived, and alter the Result as they see fit; alternatively
subclasses can override OnPacketReceived as they see fit.

Did I mis-interpret the question?

Marc

Aug 8 '06 #2
I think you have the right idea of what I am trying to do. I tried to
use the code you provided but received a few different errors that I am
working through now.

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)?

Thanks again!

Aug 8 '06 #3
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);
}
}
Aug 8 '06 #4
Hi,
>
What I want to do is in my second assembly, which is a c# console
application, is define the PacketOnReceive for the distinct classes.
You cannot, they are already defined. they were defined when you defined
your class.
You have various alternatives:
1- Use events
2- Derive new classes, but this may render useless the classes you already
defined unless that you still use theirs implementation
3- Use a strategic pattern, this is like a mix of both of above options

This way I can customize what happens when the function PacketOnReceive
is called. Then I could could create any number of applications based
on the first assembly that would have custom code.
What is the use of the derived classes you created ?

If you post what you really want to do we could give you a better idea how
to implement it
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 8 '06 #5
What I am doing is creating a library for data packets that I can send
from clients to a server application. The library will have all the
various packets defined, such as connect, disconnect, etc.

When a packet is received, we don't know it's type, but since all
packet types are from a base class, which has a defined header, we can
extract the packet ID. This tells us if it is a connect or discconect.

The library will create the appropriate packet based on the packet ID,
and then call on receive. Each application can handle the same packet,
but have different implementations.

A client may just print a message to the user that it connected. A
server would add the client to a list, etc. Any application using the
library could do what they want with the pack et when it is received.

Does this help to understand what I am trying to do?

Aug 8 '06 #6
Any other suggestions?

Aug 8 '06 #7
To create the packet class I first storing the type in array like this:

myArray[x] = Type.GetTypeFromHandle(Type.GetTypeHandle(packet))
Then when I get the packet I determine the ID. Then I create the
packet:

Type newPacketType = myArray[messageId];
PacketBase packetBase;
packetBase = (PacketBase)Activator.CreateInstance(newPacketType );

Aug 8 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Che | last post: by
6 posts views Thread by Pete Davis | last post: by
22 posts views Thread by Nemisis | last post: by
1 post views Thread by Sergei Shelukhin | last post: by
9 posts views Thread by Gilbert | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.