Buttons, Arrays and loops

Hi all, I’m currently learning c#. I’d like to know If this scenario possible?

So the usual way of dealing with buttons is to write the buttons action in its own function and attach that script to the button, so in one script you’d have 5 functions for 5 buttons.

Now I understand there’s probably no need for this but would it be possible to put all the buttons in an array and use a for loop or switch statement to loop through them and get each button to do it’s specific action?

Not sure if I’m making too much sense, if not please ask and I’ll try to explain further.
I’m just trying to get my head around loops and arrays so I’m trying different things (:

You could make all the buttons call the same function. Make the function expect an int as a parameter. you could then switch the int and call different methods based on the int.

You could get creative and use an enum instead of an int.

You could have a dictionary like so var buttonFunctions= new Dictionary<int, Action>(); you can do this because Action is just a delegate that represents a function with a signature of void myFunction()

You can read more about the action delegate here

But like you said there’s really no reason to do all of this. If you need to do this, it’s probably a code smell.

Here’s another question worth checking out.

Hey @stormMuller thanks for your input! really appreciate it (:

I’ll give this a crack today and get back to you with how i get on, i had a feeling enums would play a part! the one thing i havent studied yet but this is cool because it gives me an opportunity to get stuck into it.

exactly, i totally get theres no need for it, its more just exploring the possibilities and seeing how i can get creative with my coding and not just do things from tutorials.