473,322 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1717
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: takashi | last post by:
Hi, I have a question. I am learning about how to use c++ language. I have attempted to make my own programs, using the knowledge that I have, but sometimes when I get stuck on writing a code, it...
26
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I...
2
by: Mike Hennessy | last post by:
I'm looking for people's opinions and feedback regarding the design of the application tier, and how to best logically separate out the Data Access from the Business Object's. Per the Microsoft...
3
by: Che | last post by:
Hi, I am unsure of this message should be posted here. If not please let me know the appropriate group. I was wondering how i could implement my own pragma. Say #pragma to_asm_code...
7
by: Felix Kater | last post by:
Hi, when I need to execute a general clean-up procedure (inside of a function) just before the function returns -- how do I do that when there are several returns spread over the whole function?...
6
by: Pete Davis | last post by:
I'm confused about what precisely the limitations are on loading plugins in separate app domains. In all my previous apps that supported plugins, I've loaded them into the same domain as the app,...
22
by: Nemisis | last post by:
Hi everyone, i am creating my own DAL and BLL, and i am not using typed datasets. My problem is that in my DAL i have a Save method whos signiture looks something like: Save(ID as integer,...
1
by: Sergei Shelukhin | last post by:
Hi. We have a resource assembly that is separate and is used by a class library assembly, a web app, web service set (all in separate projects). Windows app is also potentially possible. First...
9
by: Gilbert | last post by:
Hi, In the code-behind, i have this function: Public Function myfunction(ByVal myvar As Object) As String dim x as string = myvar ..... Return x End Function
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.