C
C#3mo ago
mack

✅ 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 ints when called
No description
6 Replies
canton7
canton73mo ago
Given a method which might have 1, 2 or 3 parameters, how would you go about calling it (with the correct number of arguments)?
mack
mackOP3mo ago
(working in unity) i dont have the actual way i would be using this yet, but for instance,
Debug.Log(MethodName(type));
Debug.Log(MethodName(type));
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 on
canton7
canton73mo ago
The 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?
mack
mackOP3mo ago
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
Angius
Angius3mo ago
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 methods
mack
mackOP3mo ago
thank you for the info 💜
Want results from more Discord servers?
Add your server