Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function to massive setDisplay #247

Open
juanmartin84 opened this issue Dec 2, 2024 · 4 comments
Open

function to massive setDisplay #247

juanmartin84 opened this issue Dec 2, 2024 · 4 comments
Labels

Comments

@juanmartin84
Copy link

Hello, I'm trying to create a function to show/hide some cards according to the value of a variable

I wrote the following, but it doesnt work

void setOutput(String outputType, Card zone, Card zoneSlave, Card zoneAux, Card buttonZone, Card buttonZoneAux){
    if(outputType == "0"){//disable
      zoneCard.setDisplay(false);
      zoneSlaveCard.setDisplay(false);
      zoneAuxCard.setDisplay(false);
      buttonZone.setDisplay(false);
      buttonZoneAux.setDisplay(false);
      }
    if(outputType == "1"){//master
      zoneCard.setDisplay(true);
      zoneSlaveCard.setDisplay(false);
      zoneAuxCard.setDisplay(false);
      buttonZone.setDisplay(true);
      buttonZoneAux.setDisplay(false);
      }
    if(outputType == "2"){//slave
      zoneCard.setDisplay(false);
      zoneSlaveCard.setDisplay(true);
      zoneAuxCard.setDisplay(false);
      buttonZone.setDisplay(false);
      buttonZoneAux.setDisplay(false);
      }
    if(outputType == "3"){//aux
       zoneCard.setDisplay(false);
       zoneSlaveCard.setDisplay(false);
       zoneAuxCard.setDisplay(true);
       buttonZone.setDisplay(false);
       buttonZoneAux.setDisplay(true);
      }
}  

Is there some way to make a kind of function to avoid repeat all this lines for each card?

BR

@mathieucarbou
Copy link
Contributor

mathieucarbou commented Dec 2, 2024

your function definition is wrong: you are passing the cards by value. Use ref or pointers. Example:

void setOutput(const String& outputType, Card& zone, Card& zoneSlave, Card& zoneAux, Card& buttonZone, Card& buttonZoneAux){

Also, what you wan to do is better served using switch/case since conditions are mutually exclusive.

switch(outputType.toInt()) {
    case 0: {
        /// setDisplay(...)
        break;
    }
     case 1: {
        /// setDisplay(...)
        break;
    }
    default:
        break;
}

@juanmartin84
Copy link
Author

Thanks @mathieucarbou !!!
This made the code much more readable and shorter.
Following with this, is there some way to do something like this with callback functions?

@mathieucarbou
Copy link
Contributor

No because the callback does not hold the information about the card, but what you can do is call the same function.

@juanmartin84
Copy link
Author

thanks @mathieucarbou

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants