tags:

views:

57

answers:

3

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?

A: 

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

Andrey
but how can class A calls private method of class B? i want to raise event programmatically.
Azodious
+2  A: 

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.

David Lively
+2  A: 

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
+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.
David Lively