473,750 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::P acketOnReceive" );
return true;
}
}

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

public class PacketDisconnec t : PacketBase
{
public override bool PacketOnReceive ()
{
Trace.WriteLine ("PacketDisconn ect::PacketOnRe ceive");
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 1738
Generally you have 2 options, then; inheritance or events. There is no
reason that the console app can't itself subclass PacketConnect and
PacketDisconnec t 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<Pa cketEventArgsPa cketReceived;
protected virtual bool OnPacketReceive d() {
EventHandler<Pa cketEventArgsha ndler = PacketReceived;
if(handler==nul l) 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 OnPacketReceive d 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 + "::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);
}
}
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.GetTypeFro mHandle(Type.Ge tTypeHandle(pac ket))
Then when I get the packet I determine the ID. Then I create the
packet:

Type newPacketType = myArray[messageId];
PacketBase packetBase;
packetBase = (PacketBase)Act ivator.CreateIn stance(newPacke tType);

Aug 8 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3407
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 took me a long time to figure out what I should do. For instance, I was writing a program which tells you all the prime numbers that are less than the number you input on the console. It was a very short program, but it took me a while to write...
26
3150
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 had done myself a disservice by having not attempted and completed the exercises. I intend to rectify that. My current routine is to read a chapter and then attempt every exercise problem, and I find that this process is leading to a greater...
2
1832
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 prescriptive architecture documents, they recommend creating a completely separate logical Data Access Tier of components. Then creating a separate tier of Business Objects that consume them. My first question is...what does this actually buy you...
3
1611
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 {function_name} {function_name} is an assembly routine. I believe some compilers provide this feature by mechanism of "FastCall". I don't want anything complicated. All I wish to do is the language must recognize this as a
7
2243
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? My first approach: Use "while(1)" and "break", however this doesn't work if there is another loop inside (since I can't break two loops at the same time):
6
4503
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, but I've just started playing around with separate AppDomains and I'm finding that I'm not having problems where I expected I would, so maybe someone can help me understand a bit better. I've read that objects instantiated in separate AppDomains...
22
4324
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, name as string, .......
1
2437
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 of all, VS2005 generates resource wrappers as internal, so we have to use InternalsVisibleTo to make intellisense etc possible for resources. Then, with this model, all the cool localization features of ASP.NET 2.0 are not possible. Is there any...
9
1777
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
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9394
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.