446,361 Members | 1,800 Online
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

# Delegate question

 P: n/a 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 of the functions it refers to. So I've been playing around and came up with he following code. I know it doesn't do much, but I just wanted to fit together delegates and parameter passing: //////////////// Code start public delegate int Sort(int a, int b, int c); public class SortProgram { public static int doSort(Sort ar, int a, int b, int c) { return(ar(a, b,c)); } public static int highest(int first, int second, int third) { int tmp = 0; if (first > second) tmp = first; else tmp = second; if (tmp > third) return tmp; else return third; } public static int lowest(int first, int second, int third) { int tmp = 0; if (first < second) tmp = first; else tmp = second; if (tmp < third) return tmp; else return third; } public static int middle(int first, int second, int third) { int tmp = 0; if (first > second) tmp = first; else tmp = second; if (tmp > third) return third; else return second; } public static void Main() { int a = 90; int b = 110; int c = 300; Sort high = new Sort(highest); Sort low = new Sort(lowest); Sort mid = new Sort(middle); Console.WriteLine("Select highest of {0}, {1}: {2}", a, b, doSort(high, a, b, c)); Console.WriteLine("Select lowest of {0}, {1}: {2}", a, b, doSort(low, a, b, c)); Console.WriteLine("Select middle of {0}, {1}: {2}", a, b, doSort(middle, a, b, c)); } } //////////////// Code end Regarding the code, is this how one would go about using delegetes for such a function? Or am I doing something wrong / being long winded with my code? Is it ok to declare a delegete in the namespace (I figured other classes could use it if they needed to), or is it best to keep such declarations to class body? I gave up on events until I get my head around this, they're on my list next :). Any tips, or pointers would be welcomed. Chris Dec 14 '05 #1
4 Replies

 P: n/a MisterJingo, What you are doing is pretty much right. The only thing that I would think is excessive is the call to doSort. You don't need to wrap the delegate in a method call, you can just call the delegate directly. Other than that, it looks fine. The nature of the delegate will determine where I declare it. If I have a delegate which is specific to the class that will use it, then I will declare it in the class. However, for general event handlers (like EventHandler, for instance), I will declare that in a namespace. Hope this helps. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com "^MisterJingo^" wrote in message news:11*********************@g49g2000cwa.googlegro ups.com... 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 of the functions it refers to. So I've been playing around and came up with he following code. I know it doesn't do much, but I just wanted to fit together delegates and parameter passing: //////////////// Code start public delegate int Sort(int a, int b, int c); public class SortProgram { public static int doSort(Sort ar, int a, int b, int c) { return(ar(a, b,c)); } public static int highest(int first, int second, int third) { int tmp = 0; if (first > second) tmp = first; else tmp = second; if (tmp > third) return tmp; else return third; } public static int lowest(int first, int second, int third) { int tmp = 0; if (first < second) tmp = first; else tmp = second; if (tmp < third) return tmp; else return third; } public static int middle(int first, int second, int third) { int tmp = 0; if (first > second) tmp = first; else tmp = second; if (tmp > third) return third; else return second; } public static void Main() { int a = 90; int b = 110; int c = 300; Sort high = new Sort(highest); Sort low = new Sort(lowest); Sort mid = new Sort(middle); Console.WriteLine("Select highest of {0}, {1}: {2}", a, b, doSort(high, a, b, c)); Console.WriteLine("Select lowest of {0}, {1}: {2}", a, b, doSort(low, a, b, c)); Console.WriteLine("Select middle of {0}, {1}: {2}", a, b, doSort(middle, a, b, c)); } } //////////////// Code end Regarding the code, is this how one would go about using delegetes for such a function? Or am I doing something wrong / being long winded with my code? Is it ok to declare a delegete in the namespace (I figured other classes could use it if they needed to), or is it best to keep such declarations to class body? I gave up on events until I get my head around this, they're on my list next :). Any tips, or pointers would be welcomed. Chris Dec 14 '05 #2

 P: n/a Hi, Well the code use delegates correctly, you have a procedure from which you know the signature but that can be different implementations, you wait until runtime to select the correct alternative. Take a look at the Strategy pattern: http://en.wikipedia.org/wiki/Strategy_pattern cheers, -- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation "^MisterJingo^" wrote in message news:11*********************@g49g2000cwa.googlegro ups.com... 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 of the functions it refers to. So I've been playing around and came up with he following code. I know it doesn't do much, but I just wanted to fit together delegates and parameter passing: //////////////// Code start public delegate int Sort(int a, int b, int c); public class SortProgram { public static int doSort(Sort ar, int a, int b, int c) { return(ar(a, b,c)); } public static int highest(int first, int second, int third) { int tmp = 0; if (first > second) tmp = first; else tmp = second; if (tmp > third) return tmp; else return third; } public static int lowest(int first, int second, int third) { int tmp = 0; if (first < second) tmp = first; else tmp = second; if (tmp < third) return tmp; else return third; } public static int middle(int first, int second, int third) { int tmp = 0; if (first > second) tmp = first; else tmp = second; if (tmp > third) return third; else return second; } public static void Main() { int a = 90; int b = 110; int c = 300; Sort high = new Sort(highest); Sort low = new Sort(lowest); Sort mid = new Sort(middle); Console.WriteLine("Select highest of {0}, {1}: {2}", a, b, doSort(high, a, b, c)); Console.WriteLine("Select lowest of {0}, {1}: {2}", a, b, doSort(low, a, b, c)); Console.WriteLine("Select middle of {0}, {1}: {2}", a, b, doSort(middle, a, b, c)); } } //////////////// Code end Regarding the code, is this how one would go about using delegetes for such a function? Or am I doing something wrong / being long winded with my code? Is it ok to declare a delegete in the namespace (I figured other classes could use it if they needed to), or is it best to keep such declarations to class body? I gave up on events until I get my head around this, they're on my list next :). Any tips, or pointers would be welcomed. Chris Dec 14 '05 #3