Home > Net >  C # about event handling method name by default
C # about event handling method name by default

Time:11-14

Consult: custom Tester, a class which defines an event: StateChanged, new a Tester in the Form1, and +=registered StateChanged events, tip the default event handler method is called: Form1_StateChanged, that is to say the Tab after out from under the two methods for Form1_StateChanged, how to make the default method called Tester_StateChanged, rather than having to manually change each time, thank you

CodePudding user response:

Is not necessary, Forn1 don't know where you want to register the name of the function is coming from, the most reasonable, of course, is a function of their class definition (namely the default function prefix for Form1, it won't cause misunderstanding),

CodePudding user response:

CodePudding user response:

This may need to buy Microsoft. After all, "every time don't have to manually change" demand can every programmer can come up with dozens of all tastes.

CodePudding user response:

In fact, I think the.net can be modified in the configuration, even with the dynamic injection directly too much and too often, cause a variety of underlying technology debate, has a small white with other technical system competition caused the bad influence, under the past vs always provide the framework of relatively simple and straightforward a good dental laboratories test, the default parameters of medium to high, and basically don't make changes (to change will have to manually modify the registry), quite good,

In fact you can choose more concise programming concept, such as anonymous, explicit delegate, such as
 control. The evt +=(s, arg)=& gt; 
{
.
};
or
 var myProc=new EventHandlerXX ((s, arg)=& gt; 
{
.
});
Ctr1. Evt +=myProc;
Ctr2. Evt +=myProc;
.
Ctr1. Evt -=myProc;
Ctr2. Evt -=myProc;


You want to name the process, you first "independent" with the process object,

CodePudding user response:

Change can change, such as we use the Resharp myself definition template

But in fact it is not recommended, suppose I have two controls, and then,

Do you think I should write
Object1_load
Object2_load

Should be written
Form1_load1
form1_load2

CodePudding user response:

Yes, process belongs to Form1 rather than Tester, this name can explain the concept of the lz for events still have mistake,

On type Tester belong to define events, then the event interface definitions in Tester, trigger Tester, this Tester is a service, and Form1 is a host (client), is Form1 defines the processing process, the Tester only defines the interface specification,

CodePudding user response:

If you are in the new after a Tester, a host program need respectively articulated for it two or three processing, you use "press Tab twice" still can't generate, you have to write +=... This code, so don't tangle vs tools, syntax in c # to create some code style, it is good,

CodePudding user response:

A great trouble to do,

First of all, you need to understand how to extend VisualStudio, can read:
[start developing Visual Studio extension]
https://docs.microsoft.com/zh-cn/visualstudio/extensibility/starting-to-develop-visual-studio-extensions? View=v - 2019

Then, to understand the Designer host design architecture, such as can be read (marked as dotnet2.0, but the concept is the same) :
[The Create And Host The Custom Designers have The The.net Framework 2.0]
https://docs.microsoft.com/en-us/archive/msdn-magazine/2006/march/create-and-host-custom-designers-with-the-net-framework-2-0

You can then by implementing IEventBindingService interface, and the custom of CreateUniqueMethodName , as you want to according to your rules of incident response function name,

Finally, install your Visual Studio extension development, from then on, enjoy "every time don't have to manually change" happiness; -)

CodePudding user response:

refer to the eighth floor interacting in a professional developer response:
if you are in the new after a Tester, a host program need respectively articulated for it two or three processing, you use "press Tab twice" still can't generate, you have to write +=... This code, so don't tangle vs tools, syntax in c # to create some code style, good,


Sp1234 predecessors hello, often see you say event driven, then also see message driven, want to ask, 1: one of the strengths of the event driven is avoid polling check frequent operation (such as changes in values)? 2: the difference between event-driven and message-driven is what? thank you

CodePudding user response:

You can write first, in turn,
Tester. The StateChanged +=Tester_StateChanged;
Then because Tester_StateChanged didn't, so the code completion, generate a Tester_StateChanged empty methods

CodePudding user response:

Event "advantage" this simple spoken also really is all tastes, and when you do project, doing it architecture is so "natural", when you design an object that you want to design extension interface, natural with the entrusted property, and then use more standard event syntax, if write
 public StateChanged XXX; 
(where XXX is a delegate) is defined, then any host can write code
 Tester. The StateChanged=aaa; 
to tamper with the commissioned, and keyword tells the compiler, the Event is not allowed to modify the entrust, can only use +=, -=a small amount of grammar, such as protecting the safety,

Delegate object, in fact, after the compilation becomes a kind of "in line with the delegate list", that is to say, though definition StateChanged is a delegate, truly generate c # code is actually a "mandate" object, can insert multiple instances of the object to it, this is also convenient for the realization of the Event,

Said from design patterns, the event is the standard form of notification, however, if you see "design patterns" the author of the book the gang of four (GOF) does not know the event mechanism in the 90 s (though at that time Microsoft vb 1.0 event mechanism for DOS is already used to architectural interface development mode), so it wrote 20 several patterns, in fact, if use the event, so the book can be reduced 80% at least, is the party as a service initiative to callback, notice, client host, it goes with the host code polling, not desperate to shock people service is on the contrary, once there are accurate and timely notification mechanism, the latter will be reduced to one percent, one over one thousand, it took a long time a polling,

Software engineering design do not code for the code, c # code is to advance software engineering concept, but the software engineering concept than a programming language, when c # syntax from the good software engineering demand too far, we might discard the c # language and to choose a better language, so to understand the mechanism of "event", first look at the various programming language is how to define and use the event, don't entangled with only one language, apart from some kind of programming language and writing code according to the engineering design, can better use the programming language,

CodePudding user response:

Composite delegate list


I often made an example WeChat, WeChat hypothesis has 200000 servers, each user even billions of users on A server, when the user to the user B sends the message, the message finally pushed to the user terminal B, visible only server to terminal B jump events, and some of the training course on "project" to actually say WeChat is B every 1 seconds to polling the server to get messages, as this is only A practice, visible if tens of thousands of users are 1 second polling an "A" server, so don't say the server, network already paralyzed at this time, let alone 200000 servers?

Event is the most basic design, which can control the beginner of that kind of message when the thinking of "upside down", as if you eat from being duck to choose their own nutrition, is a great progress,

CodePudding user response:

+=time change, and it is not under no less trouble again, but VS the automatically generated code is ugly, if there were dozens of events, in a form that code can let people nausea, also good have a lambda and the Command pattern,
  •  Tags:  
  • C#
  • Related