M
^MisterJingo^
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
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