Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an event handler manually [duplicate]

I have an event handler method that's called directly as a standard method. That is, it's not only called when my event occurs but also just as a private method.

UtilStk.StkRoot.OnStkObjectAdded += new 
    IAgStkObjectRootEvents_OnStkObjectAddedEventHandler(TallyScenarioObjects);

private void TallyScenarioObjects(object sender)
{
    ...
}

Is it suitable to pass a null argument when calling this handler directly?

TallyScenarioObjects(null);
like image 754
wulfgarpro Avatar asked Jan 31 '11 22:01

wulfgarpro


People also ask

How do you call an event handler method?

Call an event handler using AddHandler Make sure the event is declared with an Event statement. Execute an AddHandler statement to dynamically connect the event-handling Sub procedure with the event.

How do you call an event handler in C#?

Event handlers in C# An event handler in C# is a delegate with a special signature, given below. The first parameter (sender) in the above declaration specifies the object that fired the event. The second parameter (e) of the above declaration holds data that can be used in the event handler.

What is EventArgs E in C#?

EventArgs e is a parameter called e that contains the event data, see the EventArgs MSDN page for more information. Object Sender is a parameter called Sender that contains a reference to the control/object that raised the event.

Which is the best place to subscribe event handler to an event?

To subscribe to events by using the Visual Studio IDEOn top of the Properties window, click the Events icon. Double-click the event that you want to create, for example the Load event. Visual C# creates an empty event handler method and adds it to your code. Alternatively you can add the code manually in Code view.

What happens if an event occurs and there is no event handler to respond to the event?

TF: if an event occurs and there is not event handler to respond to that event, the event ins ignored.


2 Answers

Just encapsulate the common logic into another method that can be called from your event handler:

UtilStk.StkRoot.OnStkObjectAdded += new IAgStkObjectRootEvents_OnStkObjectAddedEventHandler(TallyScenarioObjects);

private void TallyScenarioObjects(object sender)
{
    DoStuff();
}

private void DoStuff() { ... }

private void AnotherMethod()
{
    DoStuff();
}

That said, your handler is a method, there's nothing special about it, so you could always dummy up arguments and call it directly. I wouldn't go that route though.

like image 179
Ed S. Avatar answered Sep 30 '22 08:09

Ed S.


Yes that would work, but it would be better practice to have a 2nd method that could be called directly or from the event handler:

UtilStk.StkRoot.OnStkObjectAdded += new IAgStkObjectRootEvents_OnStkObjectAddedEventHandler(TallyScenarioObjects);

private void TallyScenarioObjects(object sender)
{
     DoTally(....);
}

private void DoTally(....)
{
}

If nothing else you won't confuse other developers who won't be expecting to see an event handler called that way.

like image 44
ChrisF Avatar answered Sep 30 '22 08:09

ChrisF