Click here to Skip to main content
15,900,648 members
Home / Discussions / C#
   

C#

 
GeneralRe: Exceptions across invoke Pin
richasea25-Mar-10 5:40
richasea25-Mar-10 5:40 
GeneralRe: Exceptions across invoke Pin
Keith Barrow25-Mar-10 6:17
professionalKeith Barrow25-Mar-10 6:17 
Questionproblem accessing a control from a thread (the callback is in another user defined class) Pin
George Nistor25-Mar-10 2:57
George Nistor25-Mar-10 2:57 
AnswerRe: problem accessing a control from a thread (the callback is in another user defined class) Pin
Covean25-Mar-10 3:33
Covean25-Mar-10 3:33 
GeneralRe: problem accessing a control from a thread (the callback is in another user defined class) Pin
George Nistor25-Mar-10 3:45
George Nistor25-Mar-10 3:45 
GeneralRe: problem accessing a control from a thread (the callback is in another user defined class) Pin
Covean25-Mar-10 3:59
Covean25-Mar-10 3:59 
QuestionHow to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 1:42
igalep13225-Mar-10 1:42 
AnswerRe: How to get values after dictionary sorting by values with linq Pin
Not Active25-Mar-10 1:46
mentorNot Active25-Mar-10 1:46 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 1:56
igalep13225-Mar-10 1:56 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 2:06
igalep13225-Mar-10 2:06 
AnswerRe: How to get values after dictionary sorting by values with linq Pin
Keith Barrow25-Mar-10 2:02
professionalKeith Barrow25-Mar-10 2:02 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 2:07
igalep13225-Mar-10 2:07 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 5:04
igalep13225-Mar-10 5:04 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
Keith Barrow25-Mar-10 5:25
professionalKeith Barrow25-Mar-10 5:25 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 14:27
igalep13225-Mar-10 14:27 
AnswerRe: How to get values after dictionary sorting by values with linq Pin
lukaszbl25-Mar-10 2:04
lukaszbl25-Mar-10 2:04 
QuestionDelegates - What is the use of it? Pin
Praveen Raghuvanshi25-Mar-10 1:19
professionalPraveen Raghuvanshi25-Mar-10 1:19 
Hi,

Just a basic question on delegates as I am unable to understand the exact use of it.
can anyone explain with a simple example what is the use of it?
Read that its being used to call functions at runtime?
- If we can call the methods of a class directly through its objects like obj.Method(), how do we achieve indirection using delegates.
- How can we achieve loose coupling or a communication between 2 classes through delegates.

Below is the sample code, i wrote in order to understand, but not able to.

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace DelegatesConsoleApp
7 {
8    class Program
9    {
10        static void Main(string[] args)
11        {
11            A a = new A();
12            B b = new B();
13
14            MyDelegate[] del = new MyDelegate[3];
15            del[0] = new MyDelegate(a.MethodA);
16            del[1] = new MyDelegate(b.MethodB);
17            del[2] = new MyDelegate(CStatic.Method_CStatic);
18
19            del[0]();
20            Console.WriteLine();
21            del[1]();
22            Console.WriteLine();
23            del[2]();
24
25            Console.ReadKey();
26        }
27    }
28
29    delegate void MyDelegate();
30    
31    class A
32    {
33
34        public A()
35        {
36            Console.WriteLine("Constructor of 'A' called");
37        }
38
39        public void MethodA()
40        {
41            Console.WriteLine("MethodA() of 'A' called");
42        }
43    }
44
45    class B
46    {
47        public B()
48        {
49            Console.WriteLine("Constructor of 'B' called");
50        }
51
52        public void MethodB()
53        {
54            Console.WriteLine("Method_B() of 'B' called");
55        }
56    }
57
58    class CStatic
59    {
60        public CStatic()
61        {
62            Console.WriteLine("Constructor of 'CStatic' called");
63        }
64
65        public static void Method_CStatic()
66        {
67            Console.WriteLine("Method_CStatic() of 'CStatic' called");
68        }
69    }
70 }


In above code, at lin # 15, 16, 17 we are assigning methods to the delegate, it means we need to create an object of class and call their methods. Why is the advantage of delegate of using delegate here as we can directly call the methods as we are passing it into delegate.

Thanks in advance.
Praveen Raghuvanshi
Software Engineer,

India.

AnswerRe: Delegates - What is the use of it? Pin
harold aptroot25-Mar-10 1:38
harold aptroot25-Mar-10 1:38 
AnswerRe: Delegates - What is the use of it? [modified] Pin
Keith Barrow25-Mar-10 1:39
professionalKeith Barrow25-Mar-10 1:39 
GeneralRe: Delegates - What is the use of it? Pin
Not Active25-Mar-10 1:44
mentorNot Active25-Mar-10 1:44 
GeneralRe: Delegates - What is the use of it? Pin
DaveyM6925-Mar-10 1:48
professionalDaveyM6925-Mar-10 1:48 
GeneralRe: Delegates - What is the use of it? Pin
Keith Barrow25-Mar-10 1:56
professionalKeith Barrow25-Mar-10 1:56 
QuestionCrystal Report Pin
Syed Shahid Hussain25-Mar-10 1:09
Syed Shahid Hussain25-Mar-10 1:09 
QuestionGet SQL Server 2005 Database Table Details Dynamically Pin
thanushkag25-Mar-10 0:30
thanushkag25-Mar-10 0:30 
AnswerRe: Get SQL Server 2005 Database Table Details Dynamically Pin
Mycroft Holmes25-Mar-10 1:04
professionalMycroft Holmes25-Mar-10 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.