The following piece of code (between comment /***************/ line )works fine. You can test is in console/win form/web page. Just print strResult. /**************************************************/ public delegate double DlgFn(double x); public static double f1(double x) { return Math.Exp(-(x * x)); //function to be integrated by Simpson's rule } public class Integral { public static double Integrate(DlgFn f, double a, double b, int n) { double h = (b - a) / n;//step_size double sum = 0; sum += f(a) + f(b); for (int i = 1; i <= (n - 1); i += 2) sum += 4 * f(a + i * h); for (int i = 2; i <= (n - 1); i += 2) sum += 2 * f(a + i * h); return Math.Round((h / 3) * sum, 5); } } string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, 100).ToString(); /**************************************************/ However it does not enable me to extend my application further. I want to replace the last line of code, string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, 100).ToString(); with the following lines: double lLim= 0; double uLim= 1; double n =100; // step no. DlgFn myFn = new DlgFn(Integral.Integrate); string strResult =myFn(f1, lLim, uLim, new).ToString(); The reason for doing so is because the variables, lLim, uLim and n will be used in other methods in application code. I eagerly look forward for any useful hints/help that could get me move ahead. Thanks,
[quoted text, click to view] On 9 May, 03:20, "ASP.NET explorer" <aspdotnet@learner> wrote: > The following piece of code (between comment /***************/ line )works > fine. You can test is in console/win form/web page. Just print strResult. > > /**************************************************/ > public delegate double DlgFn(double x); > > public static double f1(double x) { > return Math.Exp(-(x * x)); //function to be integrated by Simpson's > rule > } > public class Integral { > public static double Integrate(DlgFn f, double a, double b, int n) { > double h = (b - a) / n;//step_size > double sum = 0; > sum += f(a) + f(b); > for (int i = 1; i <= (n - 1); i += 2) sum += 4 * f(a + i * h); > for (int i = 2; i <= (n - 1); i += 2) sum += 2 * f(a + i * h); > return Math.Round((h / 3) * sum, 5); > } > } > string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, > 100).ToString(); > /**************************************************/ > > However it does not enable me to extend my application further. > > I want to replace the last line of code, > string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, 100).ToString(); > with the following lines: > > double lLim= 0; double uLim= 1; > double n =100; // step no. > DlgFn myFn = new DlgFn(Integral.Integrate); > string strResult =myFn(f1, lLim, uLim, new).ToString(); > > The reason for doing so is because the variables, lLim, uLim and n will be > used in other methods in application code. > > I eagerly look forward for any useful hints/help that could get me move > ahead. > > Thanks,
I've defined a second delegate with a signature identical to your Integrate method. Does that help? Test2 is the meat and veg. using System; namespace RunLotsOfThreads2 { public class Integral { public delegate double DlgFn(double x); public delegate double DlgFn2(DlgFn f, double a, double b, int n); public static double f1(double x) { return Math.Exp(-(x * x)); //function to be integrated by Simpson's rule } public static double Integrate(DlgFn f, double a, double b, int n) { double h = (b - a) / n;//step_size double sum = 0; sum += f(a) + f(b); for (int i = 1; i <= (n - 1); i += 2) sum += 4 * f(a + i * h); for (int i = 2; i <= (n - 1); i += 2) sum += 2 * f(a + i * h); return Math.Round((h / 3) * sum, 5); } public static void test() { string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, 100).ToString(); Console.WriteLine(strResult); } public static void test2() { double lLim= 0; double uLim= 1; int n =100; // step no. DlgFn2 myFn = new DlgFn2(Integral.Integrate); string strResult =myFn(new DlgFn(Integral.f1), lLim, uLim, n).ToString(); Console.WriteLine(strResult); } } }
Still can't grasp why two delegates? The code should somehow be more crisp. As I am newbie to using delegates, I was having hard time. But your code did solve my problem and so did help me move ahead for now. Many thanks ! [quoted text, click to view] "DeveloperX" <nntpDev@operamail.com> wrote in message news:1178713562.608062.91280@y5g2000hsa.googlegroups.com... > On 9 May, 03:20, "ASP.NET explorer" <aspdotnet@learner> wrote: >> The following piece of code (between comment /***************/ >> line )works >> fine. You can test is in console/win form/web page. Just print strResult. >> >> /**************************************************/ >> public delegate double DlgFn(double x); >> >> public static double f1(double x) { >> return Math.Exp(-(x * x)); //function to be integrated by >> Simpson's >> rule >> } >> public class Integral { >> public static double Integrate(DlgFn f, double a, double b, int >> n) { >> double h = (b - a) / n;//step_size >> double sum = 0; >> sum += f(a) + f(b); >> for (int i = 1; i <= (n - 1); i += 2) sum += 4 * f(a + i * >> h); >> for (int i = 2; i <= (n - 1); i += 2) sum += 2 * f(a + i * >> h); >> return Math.Round((h / 3) * sum, 5); >> } >> } >> string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, >> 100).ToString(); >> /**************************************************/ >> >> However it does not enable me to extend my application further. >> >> I want to replace the last line of code, >> string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, >> 100).ToString(); >> with the following lines: >> >> double lLim= 0; double uLim= 1; >> double n =100; // step no. >> DlgFn myFn = new DlgFn(Integral.Integrate); >> string strResult =myFn(f1, lLim, uLim, new).ToString(); >> >> The reason for doing so is because the variables, lLim, uLim and n will >> be >> used in other methods in application code. >> >> I eagerly look forward for any useful hints/help that could get me move >> ahead. >> >> Thanks, > > I've defined a second delegate with a signature identical to your > Integrate method. Does that help? Test2 is the meat and veg. > > using System; > namespace RunLotsOfThreads2 > { > public class Integral > { > public delegate double DlgFn(double x); > public delegate double DlgFn2(DlgFn f, double a, double b, int n); > > public static double f1(double x) > { > return Math.Exp(-(x * x)); //function to be integrated by Simpson's > rule > } > > > public static double Integrate(DlgFn f, double a, double b, int n) > { > double h = (b - a) / n;//step_size > double sum = 0; > sum += f(a) + f(b); > for (int i = 1; i <= (n - 1); i += 2) sum += 4 * f(a + i * h); > for (int i = 2; i <= (n - 1); i += 2) sum += 2 * f(a + i * h); > return Math.Round((h / 3) * sum, 5); > } > public static void test() > { > string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, > 100).ToString(); > Console.WriteLine(strResult); > } > public static void test2() > { > double lLim= 0; double uLim= 1; > int n =100; // step no. > DlgFn2 myFn = new DlgFn2(Integral.Integrate); > string strResult =myFn(new DlgFn(Integral.f1), lLim, uLim, > n).ToString(); > Console.WriteLine(strResult); > } > } > } >
On Wed, 09 May 2007 16:10:15 -0700, ASP.NET explorer <aspdotnet@learner> [quoted text, click to view] wrote: > Still can't grasp why two delegates? The code should somehow be more > crisp.
I admit, I'm not really clear on what issue it is you're trying to solve. But if the reply from DeveloperX solved it, perhaps it really was as simple as having to declare a second delegate type. If so, then the answer to "why two delegates" is that the delegate type requires that the "signature" of the method used to create the new delegate matches exactly with that declared in the delegate type. The "signature" being things like the return value and the parameter list. You can't use the first delegate type you declared with any method except one that returns a double and takes exactly one double as a parameter. If you want to wrap the Integrate() method in a delegate, you need a new delegate type that matches *that* method's signature (returns a double, takes as parameters one DlgFn delegate, two doubles, and an int). If you still have questions, perhaps you could try to be a little more clear about how it is you want the code to work and why it is the code you've already got doesn't do what you want.
[quoted text, click to view] "ASP.NET explorer" <aspdotnet@learner> wrote in message news:OWpFYCekHHA.4596@TK2MSFTNGP05.phx.gbl...
<snip> public static double Integrate(DlgFn f, double a, double b, int n) > [quoted text, click to view] > I want to replace the last line of code, > string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, > 100).ToString(); > with the following lines: > > double lLim= 0; double uLim= 1; > double n =100; // step no. > DlgFn myFn = new DlgFn(Integral.Integrate); > string strResult =myFn(f1, lLim, uLim, new).ToString();
I think you got all dyslexic when you slapped this together First you try to create a DlgFn out of Integral.Integrate which has the wrong signature. Then instead of passing myFn into Integrate you passed f1 to myFn Then you passed 'new' instead of 'n' as your last param Let me attempt to disentangle I assume that you meant some thing like this DlgFn myFn = new DlgFn(f1) string strResult =Integral.Integrate(myFn , lLim, uLim, n).ToString(); but that has a problem as well. The last param to Integrate takes an int and you are passing a double. So here we go again DlgFn myFn = new DlgFn(f1) string strResult =Integral.Integrate(myFn , lLim, uLim, (int)n).ToString(); Is that what you were looking for? Bill
Hi Bill, A belated thanks for suggestions. All of you put me on right path and I am moving ahead with some knowledge. ---- [quoted text, click to view] "Bill Butler" <qwerty@asdf.com> wrote in message news:y9Q0i.22018$pW5.3871@trnddc07... > "ASP.NET explorer" <aspdotnet@learner> wrote in message > news:OWpFYCekHHA.4596@TK2MSFTNGP05.phx.gbl... > <snip> > public static double Integrate(DlgFn f, double a, double b, int n) > >> I want to replace the last line of code, >> string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, >> 100).ToString(); >> with the following lines: >> >> double lLim= 0; double uLim= 1; >> double n =100; // step no. >> DlgFn myFn = new DlgFn(Integral.Integrate); >> string strResult =myFn(f1, lLim, uLim, new).ToString(); > > I think you got all dyslexic when you slapped this together > > First you try to create a DlgFn out of Integral.Integrate which has the > wrong signature. > Then instead of passing myFn into Integrate you passed f1 to myFn > Then you passed 'new' instead of 'n' as your last param > > Let me attempt to disentangle > > I assume that you meant some thing like this > DlgFn myFn = new DlgFn(f1) > string strResult =Integral.Integrate(myFn , lLim, uLim, n).ToString(); > > but that has a problem as well. > The last param to Integrate takes an int and you are passing a double. > So here we go again > > DlgFn myFn = new DlgFn(f1) > string strResult =Integral.Integrate(myFn , lLim, uLim, > (int)n).ToString(); > > Is that what you were looking for? > > Bill >
Don't see what you're looking for? Try a search.
|