0

When I define my event inside a class I know what the parameters will be. My question is, in C#, can different class functions that get bound to this event either not define any parameters or define them as a dynamic type so that things act more like a scripted language (say Lua).

pseudo code to show the idea.

class Input
{
   MyEvent<int, int> onKeyDown;

   public void Update()
   {
     if(KeyHit())
        onKeyDown.Raise(key, ctrl);
   }
}

class Controller
{
   // notice how we don't care about the parameters the event is sending so we don't define anything. this is what I'm curious if it can happen
   public void Move()
   {
   }
}

Input input = new Input()
Controller c = new Controller()

input.onKeyDown += controller.Move

What would be the closest thing you can get to having this work?

Or maybe I only care about the key and not the ctrl so I define Move() with only the first parameter?

user441521
  • 6,942
  • 23
  • 88
  • 160
  • Closest thing might be: `input.onKeyDown += (_, __) => controller.Move();` or `input.onKeyDown += (key, _) => controller.Move(key);` and so on – Evk Mar 02 '17 at 19:24
  • What is the _ or __ meaning? – user441521 Mar 02 '17 at 19:29
  • That is just regular variable names often used in different languages to express that you do not care about those variables and are not going to use them, but are forced to declare them (in this case to match signature of event). If you don't like that idea, just use any other names you like: `input.onKeyDown += (key, ctrl) => controller.Move();` – Evk Mar 02 '17 at 19:32
  • Ah, I see, so the idea is to satisfy the contract ourselves and then call the function. So no way to have C# figure this out for us dynamically? – user441521 Mar 02 '17 at 19:34
  • Nope, it's statically typed language after all. But at least we can do that with short syntax and still have our Move method be defined without unnecessary arguments. – Evk Mar 02 '17 at 19:37
  • Events require a delegate instance that matches the declared signature _exactly_. See the marked duplicate for discussion and answers regarding how to adapt the declared signature to an existing method with a different number and/or type of parameters. – Peter Duniho Mar 02 '17 at 19:38
  • Right, it would just require 2 changes if I decide I want them later. Change the function to accept them and then change the place where I sub to the event. Not the worst but was hoping for just the 1 change to the function sig. – user441521 Mar 02 '17 at 19:38
  • Since functions can have a variable number of params you couldn't make them dynamic types? Seems like they have concepts in place to make something like this happen. – user441521 Mar 02 '17 at 19:43
  • This is valid: public void Test(params dynamic[] values), and then I can pass them in via: Test("what", 5, 12.0). The question would be having an event system that allows type params dynamic I guess? – user441521 Mar 02 '17 at 19:47
  • Function cannot really have variable number of params in C#. There are some syntax constructs that might give the such impression, but in the end all functions have fixed number of params. with "params" function has one parameter which is array. – Evk Mar 02 '17 at 19:47
  • Actually it looks like it sort of be done, not in the duplicate answer because that's from 2010. Create a delegate that takes params dynamic[]. Then all function sigs should take params dynamic[] as well. This will satisfy things and now you can send different types for each param and the callback function itself deals with the params if they want to. It works and is dynamic enough I guess. – user441521 Mar 02 '17 at 20:03
  • Yes but you lose all static typing completely (and all your functions will have one parameter which is array of dynamic objects). If that's fine for you then why not. I personally will not give away such valuable thing because you gain massive inconvenience for just a tiny bit convenience :) – Evk Mar 02 '17 at 20:13
  • I work in static type languages and scripting languages that don't have static typing and from my perspective static typing isn't all that valuable in my eyes and often causes flexibility issues. I used to have the opposite view of that but I've changed over time. Good to know it's in the back pocket if I need it I guess. Thanks for bouncing ideas around. – user441521 Mar 02 '17 at 20:51
  • Just noticed with dynamic you don't lose the static typing as they do get typed at run-time, but they can obviously change. However given one central place raising the event and they are params not normal member fields this seems like they would most likely always be the right type anyway. So really then it's about accessing via array index vs it's own variable. – user441521 Mar 02 '17 at 21:05
  • I wish this wasn't marked a duplicate because I have a slightly different solution than what it was marked but can't put it. – user441521 Mar 02 '17 at 21:19

0 Answers0