1

i've two classes. One class (say A) takes a textbox in c'tor. and registers TextChanged event with private event-handler method. 2nd class (say B) creates the object of class A by providing a textbox.

how to invoke the private event handler of class A from class B?

it also registers the MouseClick event.

is there any way to invoke private eventhandlers?

Azodious
  • 13
  • 3

3 Answers3

3

Short answer: don't.

Declare your event handler as public or, better yet, create a public proxy method, like

public class MyClass 
{
  private myHandler....

  public returnType DoClick() { return myHandler(...); }  
}

Giving direct access to a private member defeats the purpose of declaring it private.

3Dave
  • 28,657
  • 18
  • 88
  • 151
2

Create a public method that both the event handler and the other class can call. In general, it's a bad idea to call event handlers directly. Think carefully about what you're trying to do and you should be able to find a code structure that more closely matches the concept of what you're trying to do. You don't want your other class to click a button; you want your other class to do something that clicking a button will also do.

Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
  • 1
    +1 Nice semantic analysis. If the class in question isn't a UI component (page or control), it shouldn't be exposing handlers, but the methods that the handlers would call. – 3Dave Jun 17 '10 at 15:03
0

there is no restriction on both subscribing with private method and firing event with private subscriber. Did you have any errors so far?

Andrey
  • 59,039
  • 12
  • 119
  • 163