Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What type of method is an "On" method of an event

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I am looking at events and how they are structured differently between web pages and non web pages.

In a web page we must have a method that takes the event name and precedes the name with “On”. So in my example below the event is “StageCompleted”. So we need a special public method called “OnStageCompleted”. As a matter of fact, if you create a user control with a method called “OnStageCompleted” but without the event line – it doesn’t even show in intellisense. As soon as you add the line:

Code:
public event StageCompletedHandler StageCompleted;

it shows up in intellisense in the user control in the .aspx page. It will work fine there. But where is defined. I am not even sure what is called? It isn’t a delege, or event – so what is it?

If I were to change it to “ForStageComplete”, it would build and run fine but wouldn’t work because it didn't start with "On".

For a basic event , (in dotnet), you don’t need the special “OnStageCompleted” method, you only need to do the following (if not being used on a web page)

1) A Delegate: We need a way to pass data from the event to the user of the event, which may or may not be a web page. If nothing being passed you can use the already defined delegate – EventArgs.
2) An Event: usually called handler. Something for the user to subscribe to.
3) Code to Raise the Event (execute the procedure defined by the user).

So we can define the argument array the delegate will use we could:
Code:
public class StageCompletedEventArgs : EventArgs
{
    private int productID;
    public int ProductID
    {
        get { return productID; }
        set { productID = value; }
    }
    private int stage;
    public int Stage
    {
        get { return stage; }
        set { stage = value; }
    }
}

Then you would have a delegate which will be used by the event:

Code:
public delegate void StageCompletedHandler(object sender,
        StageCompletedEventArgs e);

In the class we define the event that will use the delegate and in the class we can have a method that runs and will raise the event by calling the event name.

Code:
public class AssemblyLine
{
    public event StageCompletedHandler StageCompleted;

    public void StartAssemblyLine(int prodID)
    {
        for (int stage = 1; stage < 6; stage++)
        {
            System.Threading.Thread.Sleep(2000);

            if (StageCompleted != null)
            {
                StageCompletedEventArgs args = new StageCompletedEventArgs();
                args.ProductID = prodID;
                args.Stage = stage;
                // Raise the event
                StageCompleted(this, args);
            }
        }
    }
 }

This would be called from another class something like:
Code:
AssemblyLine al = new AssemblyLine();
al.StageCompleted += new StageCompletedHandler(handleStage);
al.StartAssemblyLine(Int32.Parse(txtProdID.Text));

This would go to a method “al2_StageCompleted” with no “OnStageCompleted” defined.

On a web page it would be something like:
Code:
<uc1:Assembly2 ID="Assembly2" OnStageCompleted="handleStage" runat="server" />

But in this case the “OnStageCompleted method would need to be added to do what I was doing in my other method so a web page could call it:

Code:
protected void OnStageCompleted(int prodID, int stage)
{
    if (StageCompleted != null)
    {
        StageCompletedEventArgs args = new StageCompletedEventArgs();
        args.ProductID = prodID;
        args.Stage = stage;
        // Raise the event
        StageCompleted(this, args);
    }
}

My question is what is this method - it isn't the event?

Also, why do you have to use the word "On" to precede it. It isn't just a suggested way of doing it - you have to do it that way.

I am just curious as to where that is mentioned in MS Docs or other examples.

Thanks,
Tom

 
1. that method is fulfills the signature of the delegated event. events in webforms are not different than on the desktop.

2. the preceeding "On" is a convention. It may autowire events for you OnFoo links to Foo. however it's not required. you can name the event, delegate and handler whatever you want and wire them together
Code:
delegate DateTime Fu (string s, int i);

event Fu Foo;

Fu += Bar;

private DateTime Bar(string s, int i)
{
   logic goes here
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Actually, from what I see, it is different if you are using a web page and are subscribing to an event of a user control.

The "On" method MUST be there as far as I can see. If I were to call it "ForStageCompleted" - it would not work.

You can put it in the user control tag as "For" but it wouldn't do anything. It also would not show up in the list. As soon as you add the Event line the "OnStageCompleted" shows up in the intellisense list - even if you don't have an actual method defined. So it isn't just a convention - it seems to be a requirement.

I can't seem to find the normal wiring up line that would normally be there:

al.StageCompleted += new StageCompletedHandler(handleStage);

Normally it would be in the Designer page but it doesn't seem to be there.

Thanks,

Tom
 
this works. nothing special, just events, delegates and handlers.
Code:
using System;

internal class Program
{
    private static void Main()
    {
        var source = new Notifer();
        var observer = new Observer();

        source.ChangeInStatus += observer.Update;
        source.SendUpdates();

        Console.ReadLine();
    }
}

public delegate void StatusChange(int i);

public class Notifer
{
    public Notifer()
    {
        // null pattern to prevent null references
        ChangeInStatus += a => { };
    }

    public event StatusChange ChangeInStatus;

    public void SendUpdates()
    {
        ChangeInStatus(1);
        ChangeInStatus(2);
        ChangeInStatus(3);
    }
}

public class Observer
{
    public void Update(int x)
    {
        Console.WriteLine("updated to {0}", x);
    }
}
now whether this works with drag'n drop, WYSIWYG or other IDE cruft I can't say. I find that stuff distracting and avoid it as much as possible.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top