0

I've created WCF service along this tutorial. I'm hosting this service via windows forms application. I've done event setting like in this question. Simply I'm firing an event from service and handling it in my WF host application. After that i wanted to pass a value from the event handler so i used class which inherits from EventArgs and have additional properties. I set them in the event handler. The first answer from this question led to that, but... I assume that i can have multiple properties and set them in the event handler - and after firing the event read them - if so, I want to have more than one property. If I wanted only 1 return value i could use the second answer from last linked question. And now i must have a class which inherits from EventArgs and adds additional properties, the problem is that i have to access it from host and service project and this is a circular reference, I've tried with interface but interface cannot inherit from EventArgs. And I have warning that types might be not compatible.

To sum up:

  1. Can I set multiple properties of the class which inherit from EventArgs in the event handler, and read them in the caller? (I'm pretty sure that I can, but is it a bad practise?)
  2. How to solve this reference problem? (I read about interfaces, but I think this is not a case here)
4
  • When we analyze it, EventArgs really has nothing to do with it. After all, suppose your event had arguments that didn't inherit from EventArgs. Wouldn't the problem be exactly the same? (Side point - is there actually any need to inherit from EventArgs?) One way to avoid a circular reference is to move a class to a third project which is referenced from the other two. It's hard to say more than that without seeing some concrete details. Aug 26, 2019 at 17:12
  • A lot of event accept a state object so if you have multiple properties to change pass a state object. A state object class is passed by reference so if the event changes value the original object contains same new value. See one of the asynchronous Socket examples at msdn : learn.microsoft.com/en-us/dotnet/framework/network-programming/…
    – jdweng
    Aug 26, 2019 at 17:13
  • Hmmm... So the arguments sent like: 'public void OnSomethig(Object sender, EventArgs e, ref int x, ref int xx...)' wont be considered like bad practise? I thought that EventArgs is the object to pass some additional arguments.
    – HoneyBunny
    Aug 26, 2019 at 18:00
  • So I got the clue that It's not a good idea to add the third project only for one class which inherits from EventArgs. I will edit or answer the question, when I solve my problem. I think I should dig more about how to return or pass and change values of objects via event handler.
    – HoneyBunny
    Aug 26, 2019 at 19:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.