开发者

Calling private event handler from outside class

开发者 https://www.devze.com 2023-01-04 00:36 出处:网络
i\'ve two classes. One class (say A) takes a textbox in c\'tor. and registers Text开发者_开发知识库Changed event with private event-handlermethod.

i've two classes. One class (say A) takes a textbox in c'tor. and registers Text开发者_开发知识库Changed 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?


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消