Home > Software engineering >  Refactor the button function?
Refactor the button function?

Time:11-22

I have many of this button function, the only differenct is the GameObject[] through the singleton.

Like this

public void Oneselect()
{
   GameObject[] typeor = Manager.singleton.arrayOne; 
  dosomething();
}
public void Twoselect()
{
   GameObject[] typeor = Manager.singleton.arrayTwo;
  dosomething();
}
.
.
.
public void Tenselect()
{
   GameObject[] typeor = Manager.singleton.arrayTen;
  dosomething();
}

How can i refactor this, like only use one function?

CodePudding user response:

you can do this like that

        public void onSelect(int selectedAmount){
   switch (selectedAmount)
        {
        case 1:
                GameObject[] typeor = Manager.singleton.arrayOne;

             break;
        case 2:
               GameObject[] typeor = Manager.singleton.arrayTwo;

            break;
        case 10:
               GameObject[] typeor = Manager.singleton.arrayTen;

            break;
//optional
        default:
               GameObject[] typeor = Manager.singleton.arrayTwo;

            break;
        }
}

and call it like onSelect(HOW MANY SELECTED);

  • Related