2

I have this function.

How do I change only the parameters that aren't zero when calling the function? This is what I did

function changeParameters(uint newAnte, uint8 newNumberOfPlayers, uint newWinnerPart) {
    if (msg.sender == owner) {
         if (newAnte != 0) {
            ante = newAnte;
         }
         if (newNumberOfPlayers != 0) {
            required_number_players = newNumberOfPlayers;
         }
         if (newWinnerPart != 0) {
            winner_part = newWinnerPart;
        }
        }
}

I'm certain there has to be a more efficient way

Thanks

2 Answers 2

2

You can use require for input validation, it is more efficient than using if.

Please check this answer for more details.

Difference between require and assert and the difference between revert and throw

1

I added a constant None as suggested by another answer that was deleted. That solved my issue:

uint80 constant None = uint80(0); //null constant


function changeParameters(uint newAnte, uint8 newNumberOfPlayers, uint newWinnerPercentage) {
    // Only the creator can alter this
    if (msg.sender == owner) {
     if (newAnte != None) {
        ante = newAnte;
    }
    if (newNumberOfPlayers != None) {
        required_number_players = newNumberOfPlayers;
    }
    if (newWinnerPercentage != None) {
        winner_percentage = newWinnerPercentage;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.