Click here to Skip to main content
15,909,939 members
Home / Discussions / C#
   

C#

 
GeneralRe: About dateTimePicker problem Pin
PIEBALDconsult27-Nov-09 2:59
mvePIEBALDconsult27-Nov-09 2:59 
QuestionRe: About dateTimePicker problem Pin
ScottM127-Nov-09 3:04
ScottM127-Nov-09 3:04 
AnswerRe: About dateTimePicker problem Pin
PIEBALDconsult27-Nov-09 4:45
mvePIEBALDconsult27-Nov-09 4:45 
QuestionDelegates and Events Pin
BobInNJ26-Nov-09 12:51
BobInNJ26-Nov-09 12:51 
AnswerRe: Delegates and Events Pin
Luc Pattyn26-Nov-09 13:08
sitebuilderLuc Pattyn26-Nov-09 13:08 
AnswerRe: Delegates and Events Pin
Yuri Vital26-Nov-09 13:16
Yuri Vital26-Nov-09 13:16 
AnswerRe: Delegates and Events Pin
PIEBALDconsult26-Nov-09 15:11
mvePIEBALDconsult26-Nov-09 15:11 
AnswerRe: Delegates and Events Pin
N a v a n e e t h26-Nov-09 16:42
N a v a n e e t h26-Nov-09 16:42 
Stock class owns those events. Main is just a subscriber who will get notification when the event occurs. To get event notification, you need to subscribe to it first.
public static void Main()
{
    Stock stock1 = new Stock();
    stock1.OnAlert += AlertOccured;  // Subscribing event.
    stock1.Alert(); // This will call <code>Alert()</code> method which will trigger <code>OnAlert </code>event.
}

private void AlertOccured(object source, StockArgs a)
{
    // your code.
}
Delegates are the backbone of events. The main difference is events can be raised only by the class which declares them where a delegate instance can be invoked by anyone who have access to it. Following example shows what I meant.
public delegate void ADelegate();
class Foo
{
    public ADelegate ADelegateInstance;

    public void FireADelegate()
    {
        if (ADelegateInstance != null)
            ADelegateInstance();
    }
}

class Program
{
    static void AMethod()
    {
        Console.WriteLine("AMethod()");
    }

    static void Main(string[] args)
    {
        Foo f = new Foo();
        f.ADelegateInstance += AMethod;
        f.ADelegateInstance();   // This is valid and calls AMethod
    }
}
Now if you change public ADelegate ADelegateInstance to public event ADelegate ADelegateInstance the code won't compile. f.ADelegateInstance() is invalid here.

If you are following the standard void method(object, EventArgs) pattern for events, you don't have to create your own delegates. Use the EventHandler<T> provided with framework. Your code can be written like,
public class Stock {
     
     public event EventHandler<StockArgs> OnAlert;
     public event EventHandler<StockArgs> OnVolumeAlert;

     public void Alert()
     {
         if ( OnAlert != null )
           OnAlert( this, new StockArgs("ORCL", "14.50", 100000) );
     }
}
Smile | :)

Best wishes,
Navaneeth

GeneralRe: Delegates and Events Pin
Yuri Vital26-Nov-09 20:59
Yuri Vital26-Nov-09 20:59 
GeneralRe: Delegates and Events Pin
N a v a n e e t h27-Nov-09 0:22
N a v a n e e t h27-Nov-09 0:22 
Questionanother web browser control?!! Pin
Jassim Rahma26-Nov-09 11:56
Jassim Rahma26-Nov-09 11:56 
AnswerRe: another web browser control?!! Pin
Abhishek Sur26-Nov-09 12:01
professionalAbhishek Sur26-Nov-09 12:01 
GeneralRe: another web browser control?!! Pin
Jassim Rahma26-Nov-09 12:08
Jassim Rahma26-Nov-09 12:08 
AnswerRe: another web browser control?!! Pin
ScottM126-Nov-09 22:14
ScottM126-Nov-09 22:14 
QuestionWindows Forms Context Menu Spacer Pin
Jammer26-Nov-09 9:43
Jammer26-Nov-09 9:43 
AnswerRe: Windows Forms Context Menu Spacer Pin
DaveyM6926-Nov-09 9:48
professionalDaveyM6926-Nov-09 9:48 
GeneralRe: Windows Forms Context Menu Spacer Pin
Jammer26-Nov-09 10:34
Jammer26-Nov-09 10:34 
GeneralRe: Windows Forms Context Menu Spacer Pin
DaveyM6926-Nov-09 10:48
professionalDaveyM6926-Nov-09 10:48 
GeneralRe: Windows Forms Context Menu Spacer Pin
Jammer26-Nov-09 10:50
Jammer26-Nov-09 10:50 
QuestionMultithreading C# Pin
J imran26-Nov-09 6:09
J imran26-Nov-09 6:09 
AnswerRe: Multithreading C# Pin
Ravi Bhavnani26-Nov-09 6:28
professionalRavi Bhavnani26-Nov-09 6:28 
AnswerRe: Multithreading C# PinPopular
Abhijit Jana26-Nov-09 6:54
professionalAbhijit Jana26-Nov-09 6:54 
Questionhi Pin
bassam eshteba26-Nov-09 6:04
bassam eshteba26-Nov-09 6:04 
AnswerRe: hi Pin
Abhijit Jana26-Nov-09 6:07
professionalAbhijit Jana26-Nov-09 6:07 
AnswerRe: hi Pin
Richard MacCutchan26-Nov-09 6:52
mveRichard MacCutchan26-Nov-09 6:52 

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.