Bronek Kozicki said:
[still crossposting and asking for crosspost]
Nah, let them read it here.
Could you please provide example code in C#, with the same (or better

)
functionality as Carl's example ? It would be interesting to compare
idiom your are proposing with the one described by Carl.
Okay, we used a number of terms in the past posts ranging from polymophism
to events and delegates. What the purpose of Carl's example boils down to is
to that of a callback. In C# "delegates" are used to implement both events
and callbacks. One could argue that events basically are callbacks, let's
not get into that right now.
I say if you really want a callback, then don't use polymorphism because
using ploymorphism is a semantical statement (I like big words, please
forgive me). It implies that there is some basic entity that may have
different incarnations. In Carl's example this is not the case, there it is
plain vanilla straight ahead processing with the possibility for the client
to hook into the control flow (I am really enjoying this).
I read about delegates a couple of weeks ago in "Inside C# 2nd edition" and
grasped about half of it. The identifier names weren't all that well chosen
which made it hard to read and I thought I'd have a closer look the first
time I would need it. Which is now, you gave me the little push I needed.
I tried to stay close to the original idea of a series of tasks that should
be inaccessable from the client's side apart from one particular sub-task
that the client may replace. A string is build and the client gets the
oppurtunity to insert its piece in it. If it declines a defaul piece is used
instead.
=============== START OF CODE ===================
using System;
using System.Text;
class PlugHost
{
private string data;
public delegate void ExternalProcessorDelegate(ref string data);
public void DoProcessingAndDelegatePartOfTheWork(ExternalProcessorDelegate
custom)
{
data = "_StandardPartA_";
// (only) if the client provided its own logic,
// use that to obtain the middle part.
if (custom == null)
{
data += "_StandardPartB_";
}
else
{
string strCustom = null;
custom(ref strCustom);
data += strCustom;
}
// add another standard part
data += "_StandardPartC_";
}
public string GetResult() { return data; }
}
class App
{
static private void MyCustomProcessor(ref string s)
{
// add my two cents to the effort
s = "CustomPart";
}
static void Main()
{
PlugHost plugHost = new PlugHost();
// wrap reference to MyCustomProcessor in delegate
PlugHost.ExternalProcessorDelegate myProcessorDelegate = new
PlugHost.ExternalProcessorDelegate(MyCustomProcessor);
// call DoProcessingAndDelegatePartOfTheWork, passing the delegate
plugHost.DoProcessingAndDelegatePartOfTheWork(myProcessorDelegate);
Console.WriteLine("Result: {0}", plugHost.GetResult());
}
}
=============== END OF CODE ===================
Martin.