Hook up event c#


Lesson 14: Introduction to Delegates and Events



The Wiki

Is it possible in C to connect one event to another so emitting first event will emit second one? The only way i can see is to create a stub function that will emit second event and connect the first event to hook up event c# function. Maybe it's some decorator available or some black magic that allows to chain events without stub functions? You cannot do this, because you generally cannot do anything to an event from outside the object which owns it except for adding and removing handlers. In particular, you cannot list the existing registered handlers, and you cannot raise it.

In your case, "copying" the event is essentially the same thing in disguise, and would allow you to circumvent this restriction; therefore, it's not allowed. See this recent answer of mine for a more in-depth explanation of why things are the way they are - I just don't feel like retyping it all here. For your particular case, if you own both classes, the workaround is to make them cooperate specifically - make Ui be aware of the associated Logic instance, and add event handlers to Logic.

Of course, this introduces coupling; you can reduce it to some extent by using more generic interfaces, but you can't get rid of it entirely. As a side note, OnClick is not a good name for a. Common naming guide says that it should be simply Click and OnClick should be the name of a protected virtual method that raises it. By posting your answer, you agree to the privacy policy and terms of service.

Stack Overflow Hook up event c# Explore Jobs Documentation beta Tags Users. Sign up or log in to customize your list. Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.

Log In Sign Up. Join the Stack Overflow Community. Stack Overflow is a community of 7. Join them; it hook up event c# takes a minute: Subscribe event to event in C? Eye of Hell 8, 34 That's precisely the point. List of handlers is private to the object which owns the event, by design. Hook up event c# reason is that object alone hook up event c# control when and how the handlers are invoked. If you dating for transwomen to break encapsulation, you'll need to provide a special hatch for it, and your classes need to be aware of it - hence coupling.

What you're asking for is really hook up event c# different from "can I access the private field of one of my classes from another? So basically all i need to do is to move the subscription code into a class that owns the second event? Not sure which one of them is second: I have checked that on VS with. JaredPar k 91 The same logic works though between two objects if they are otherwise connected. Sign up or log in StackExchange. Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name. Developers Who Use Spaces Make More Money Than Those Who Use Tabs. Stack Overflow works best with JavaScript enabled.


Proper Way to Hook Up Manifold Gauges


Add a comment

Your e-mail will not be published. Required fields are marked *