473,499 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursive delegation with non-static functions?

I'm playing around with a class design using delegates and have gotten
stuck. I'm not certain if cooler heads can prevail or if it is forces
that man was not intended to tamper with.

The LazyArray class below is for processor-intensive sequences, like
recursive functions. It doesn't calculate any values that you don't
ask for and remembers the ones that it has calculated in the past so as
to not do it again. (In practice, the integer array would be replaced
with a Hashtable, I suppose, but I wanted to keep things simple for the
moment.)

The program does not compile, alas, since the static functions are
referencing items that are not in their context. I can't figure out
how to bring everything under the same tent, though. The thing that's
getting under my skin is that this DOES work if I make F and M
unrelated classes with all-static members, but as soon as I start
gathering their common functionality under a base class I'm socked with
this problem or trying to make a static virtual function.

Is there a way to untie this knot, or am I stuck?

Thanks,
Matthew
--
class Program2
{
public class LazyArray
{
public delegate int Calculate(int n);
private Calculate do_it;
private int[] known = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1 };

public LazyArray(int val_at_zero, Calculate theFunction)
{
known[0] = val_at_zero;
do_it = theFunction;
}
public int this[int n]
{
get
{
if (known[n] == -1) { known[n] = do_it(n); }
return known[n];
}
}
}

public static int f_at_n(int n) { return M[F[n - 1]]; }
public static int m_at_n(int n) { return F[M[n - 1]]; }
static void Main(string[] args)
{
LazyArray F = new LazyArray(0, f_at_n);
LazyArray M = new LazyArray(1, m_at_n);
Console.WriteLine(F[10]);
}
}

Nov 16 '05 #1
1 1840
The problem appears to be that your delegates don't take enough
arguments. You give them an integer and nothing more. You need to pass
in everything they need in order to do their jobs, or they won't be
very good delegates.

However, you can't really pass in the array, because this can get you
into infinite recursive loops. You'll have to think fairly carefully
about exactly what the delegate functions are expected to do, and
whether they see the lazy array as a LazyArray (dangerous, since this
can result in calls back to themselves) or as a simple array of ints
(dangerous, as you then expose your internal structure to the
delegates).

Nov 16 '05 #2

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

Similar topics

6
4413
by: DPfan | last post by:
Is the following so-called "delegation"? If not how to make some changes so that the F class delegates its operation to an E instance. On the other hand the following code runs without any...
6
11197
by: Ravi | last post by:
Hi, I need to find the non-recursive algorithm to find the height of a Binary Tree. Regards, SunLight.
1
1137
by: Jack | last post by:
Hi, I am writing a COM server with ATL and VC++.NET. I wonder if there is a way to reuse COM method implementations without delegation, something like this: __interface IFoo : IDispatch { ...
1
1141
by: jm | last post by:
I promise. I have tried everything I could find. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT05.asp (and much more) I have an asp (not .net) page...
2
2417
by: russell.lane | last post by:
I'm building out a pretty standard n-tier web application. The stack includes application/presentation, biz logic, and data access layers on top of an SQL server back end. We want to use...
9
3307
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
6
2823
by: Marc Castrechini | last post by:
This is a classic double hop delegation issue, however its the first time we are setting this up so we are doing something incorrectly. If we run through the IDE or using a localhost path on the...
5
1151
by: Marc Castrechini | last post by:
We are currently using impersonation with constrained delegation to run a dual server environment for ASP.NET 2.0 and SQL Server 2005. Everything is up and running great, however, we have the...
1
2365
by: abc12334 | last post by:
Im supposed to create a recursive and non recursive function to calculate this. F(n) = 1 for n = 0 or n = 1 or n=2 F(n) = F((n+1)/2)^2 + F((n-1)/2)^ 2 for n is odd F(n) = F(n/2 +...
13
1441
by: barcaroller | last post by:
What is the common way/design-pattern (if any) in C++ for delegating function calls that are not handled by a certain class. Public inheritance would be one way but not all classes are meant to...
0
7130
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
7007
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
7171
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,...
1
6893
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...
0
7386
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...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
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
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.