Click here to Skip to main content
15,887,350 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using Dotnet Login code in HTML doc Pin
BobJanova11-Jul-12 4:36
BobJanova11-Jul-12 4:36 
QuestionDelete Excel File Pin
Member 916988710-Jul-12 21:08
Member 916988710-Jul-12 21:08 
AnswerRe: Delete Excel File Pin
Richard MacCutchan10-Jul-12 21:16
mveRichard MacCutchan10-Jul-12 21:16 
AnswerRe: Delete Excel File Pin
zhxhdean11-Jul-12 20:28
zhxhdean11-Jul-12 20:28 
QuestionUnderstanding Basics About Forms Pin
AmbiguousName10-Jul-12 19:59
AmbiguousName10-Jul-12 19:59 
AnswerRe: Understanding Basics About Forms Pin
Richard MacCutchan10-Jul-12 21:15
mveRichard MacCutchan10-Jul-12 21:15 
AnswerRe: Understanding Basics About Forms Pin
lukeer10-Jul-12 21:23
lukeer10-Jul-12 21:23 
GeneralRe: Understanding Basics About Forms Pin
DaveyM6910-Jul-12 23:55
professionalDaveyM6910-Jul-12 23:55 
Delegates hold a list of delegates (called an invocation list) which are called when the delegate itself is invoked.

The += YourMethod adds a delegate to your method to the event's (an event is just a specialized delegate) invocation list.

C#
using System;

public class EventRaisingClass
{
    // holds delegates to all subscribed methods
    public event EventHandler SomethingChanged;

    public void ChangeSomething()
    {
        //
        OnSomethingChanged(EventArgs.Empty);
    }

    protected virtual void OnSomethingChanged(EventArgs e)
    {
        EventHandler eh = SomethingChanged;
        if (eh != null)
            eh(this, e);
    }
}

C#
using System;

public class SubscribingClass
{
    private EventRaisingClass eventRaisingInstance;

    public SubscribingClass()
    {
        eventRaisingInstance = new EventRaisingClass();
        // adds a delegate to the specified method to the event's invocation list
        eventRaisingInstance.SomethingChanged += EventRaisingInstanceSomethingChanged;
    }

    private void EventRaisingInstanceSomethingChanged(object sender, EventArgs e)
    {
        Console.WriteLine("Something Changed");
    }
    public void Update()
    {
        eventRaisingInstance.ChangeSomething();
    }
}


In your example the Button is the EventRaisingClass, that code is part of the framework but operates in exactly the same way.

Warning: Because a reference to an item (method) of our subscribing class is now held in another class instance, it will not be eligible garbage collection until the raising class instance is collected. If you require the subscribing instance to be eligible for collection beore that time then you can remove the delegate from the event with -= YourMethod. Not needed often, but can cause suprises if you are not aware of this.
Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: Understanding Basics About Forms Pin
AmbiguousName11-Jul-12 1:55
AmbiguousName11-Jul-12 1:55 
GeneralRe: Understanding Basics About Forms Pin
DaveyM6911-Jul-12 2:31
professionalDaveyM6911-Jul-12 2:31 
Questionfind iris image for iris recognition Pin
IqbalDani10-Jul-12 19:26
IqbalDani10-Jul-12 19:26 
AnswerRe: find iris image for iris recognition Pin
Richard MacCutchan10-Jul-12 21:14
mveRichard MacCutchan10-Jul-12 21:14 
QuestionI need to run a Exe in the Setup Install process. Pin
glennPattonWork310-Jul-12 0:06
professionalglennPattonWork310-Jul-12 0:06 
AnswerRe: I need to run a Exe in the Setup Install process. Pin
Pete O'Hanlon10-Jul-12 0:08
mvePete O'Hanlon10-Jul-12 0:08 
GeneralRe: I need to run a Exe in the Setup Install process. Pin
glennPattonWork310-Jul-12 0:12
professionalglennPattonWork310-Jul-12 0:12 
GeneralRe: I need to run a Exe in the Setup Install process. Pin
Pete O'Hanlon10-Jul-12 0:17
mvePete O'Hanlon10-Jul-12 0:17 
GeneralRe: I need to run a Exe in the Setup Install process. Pin
glennPattonWork310-Jul-12 0:35
professionalglennPattonWork310-Jul-12 0:35 
GeneralRe: I need to run a Exe in the Setup Install process. Pin
Pete O'Hanlon10-Jul-12 0:55
mvePete O'Hanlon10-Jul-12 0:55 
GeneralRe: I need to run a Exe in the Setup Install process. Pin
glennPattonWork310-Jul-12 0:58
professionalglennPattonWork310-Jul-12 0:58 
Questionmake a class for use textbox act as masketextbox Pin
Member 424148410-Jul-12 0:05
Member 424148410-Jul-12 0:05 
AnswerRe: make a class for use textbox act as masketextbox Pin
Abhinav S10-Jul-12 0:09
Abhinav S10-Jul-12 0:09 
AnswerRe: make a class for use textbox act as masketextbox Pin
Pete O'Hanlon10-Jul-12 0:16
mvePete O'Hanlon10-Jul-12 0:16 
AnswerRe: make a class for use textbox act as masketextbox Pin
Luc Pattyn10-Jul-12 0:45
sitebuilderLuc Pattyn10-Jul-12 0:45 
Questionadvanced excel Pin
Member 91698879-Jul-12 22:17
Member 91698879-Jul-12 22:17 
AnswerRe: advanced excel Pin
Eddy Vluggen10-Jul-12 4:08
professionalEddy Vluggen10-Jul-12 4:08 

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.