✅ array of methods
i am trying to create an array of methods, and those methods have parameters. is it possible to create an array with methods which may have 0, 1, and 2 parameters? they also have return values, and are meant to return
int
s when called6 Replies
Given a method which might have 1, 2 or 3 parameters, how would you go about calling it (with the correct number of arguments)?
(working in unity)
i dont have the actual way i would be using this yet, but for instance,
the reason i want the array is that i want to programmatically assign the methods to a different array (of buttons), in such a way that
Methods[0]
would be given to Buttons[0]
and so onThe reason I ask, is because that's the point that'll catch you out 🙂 If you just have a non-specific
Method
(which takes e.g. 2 parameters), how are you supposed to know how many arguments to pass it?
Do you have different types of buttons, and each type wants to call a method which takes a different number of parameters, or something?imagine the buttons as essentially a calculator for now. button 1 always adds 2 to whatever integer is passed through its method. button 2 always halves the integer passed through. there are ways around the methods with more than one parameter, but i do want one, at least
i will try another way that doesnt involve arrays and whatnot for now, thanks for your help
Methods have their type
void Foo(string s)
is of type Action<string>
int Bar(int x, double y)
is Func<int, double, int>
An array cannot store values of different types
So it will be unable to store both those methodsthank you for the info 💜