473,508 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delegate limitations?

Hi,

If I have 5-10 methods with different signatures, different content
except for the first 6 and last lines, can I use delegates to share
the 12 lines of code.

e.g.

public void method1(string p1, int p2) {
line1();
... line6();
// lines specific to method1 here
line7();
... line6();
}
public int method2(object o, myclass mc, anotherclass ac) {
line1();
... line6();
// lines specific to method2 here
line7();
... line6();
}
etc

I would like to share without making method1,method2, et al
unreadable...

thanks

Tim
Nov 16 '05 #1
3 1476
Without changing the way you're doing things there's no way to "share" the
delegates -- they are tied to the signature of the function it encapsulates.
Of course, you could encapsulate your 12 lines of code into two functions
which are called from each of your method1(), method2(), etc.

Another, more OO way to do this is to abstract away the function argument
and call semantics of the "inner stuff" that is distinct to your different
methods so you can general these operations. For example, you could create
an interface such as:

interface IExecutable
{
void Execute();
}

Then create a function

public void execute(IExecutable exe)
{
line1();
...line6();
exe.Execute(); // Execute the process passed
line7();
...line12();
}

Now you only need 1 delegate to wrap this function and you only have your 12
lines of code in a single place. What's left is, for each of your 5-10
methods, you need to implement a class that performs the "inner stuff" of
the call. The class's constructor would take the arguments you were
previously passing to the individual methods, e.g.

public class Method1Impl : IExecutable
{
public Method1Impl(string p1, int p2) { ... }
public void Execute() { /* Inner code here */ }
}

Now you can reuse your execute() function (and its corresponding delegate)
for as many processes as you want, and the only code you have to write for
each is the actual implementation code for the specific process.

Ken
"Tim Smith" <ti*******@hotmail.com> wrote in message
news:a7**************************@posting.google.c om...
Hi,

If I have 5-10 methods with different signatures, different content
except for the first 6 and last lines, can I use delegates to share
the 12 lines of code.

e.g.

public void method1(string p1, int p2) {
line1();
... line6();
// lines specific to method1 here
line7();
... line6();
}
public int method2(object o, myclass mc, anotherclass ac) {
line1();
... line6();
// lines specific to method2 here
line7();
... line6();
}
etc

I would like to share without making method1,method2, et al
unreadable...

thanks

Tim

Nov 16 '05 #2
What for?

Why not simply create two method ( or one if the 6 lines are the same &
start and end ) and you just that method(s).

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Tim Smith" <ti*******@hotmail.com> wrote in message
news:a7**************************@posting.google.c om...
Hi,

If I have 5-10 methods with different signatures, different content
except for the first 6 and last lines, can I use delegates to share
the 12 lines of code.

e.g.

public void method1(string p1, int p2) {
line1();
... line6();
// lines specific to method1 here
line7();
... line6();
}
public int method2(object o, myclass mc, anotherclass ac) {
line1();
... line6();
// lines specific to method2 here
line7();
... line6();
}
etc

I would like to share without making method1,method2, et al
unreadable...

thanks

Tim

Nov 16 '05 #3
Some of those lines are a try {} catch with pre and postamble that you
could not put into a standalone method.
"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT dot.state.fl.us> wrote in message news:<#h**************@TK2MSFTNGP11.phx.gbl>...
What for?

Why not simply create two method ( or one if the 6 lines are the same &
start and end ) and you just that method(s).

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Tim Smith" <ti*******@hotmail.com> wrote in message
news:a7**************************@posting.google.c om...
Hi,

If I have 5-10 methods with different signatures, different content
except for the first 6 and last lines, can I use delegates to share
the 12 lines of code.

e.g.

public void method1(string p1, int p2) {
line1();
... line6();
// lines specific to method1 here
line7();
... line6();
}
public int method2(object o, myclass mc, anotherclass ac) {
line1();
... line6();
// lines specific to method2 here
line7();
... line6();
}
etc

I would like to share without making method1,method2, et al
unreadable...

thanks

Tim

Nov 16 '05 #4

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

Similar topics

3
2599
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
4
1995
by: ^MisterJingo^ | last post by:
Hi all, I've been trying to get my head around delegates. The book i'm using had a single example, not much explaination, and didn't show how to set up a delegate and pass variables in and out...
7
1767
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
6
10397
by: David Veeneman | last post by:
I have several events that pass a value in their event args. One event passes an int, another a string, another a DateTime, and so on. Rather than creating a separate set of event args for each...
1
1805
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
11
2332
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
26
3591
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
7
1669
by: raylopez99 | last post by:
On Aug 16, 3:49 pm, Marc Gravell <marc.grav...@gmail.comwrote: If you cannot understand such a simple post, then you don't understand generic delegate types. Apparently you can only read your...
10
2094
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
0
7223
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,...
0
7114
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...
0
7377
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...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5045
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...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
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 ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.