0

I created a function to update some variables. I want to check if a parameter is passed or not null and then update the variable.

   function updateProfile(
        string memory _name,
        GENDER _gender
    ) public onlyOwner {
        if (_name) {
            patientInfo.name = _name;
        }
 
        if (_gender) {
            patientInfo.name = _gender;
        }
    }

2 Answers 2

2

There is no direct way to check if an input parameter is null. You'll have to check if it's the empty state of each corresponding data type. For uint check if it's 0. For string check if its bytes(string).length is 0. For address check if its address(0). For enum, this won't work because the first type is an enum is given index 0. So assuming your GENDER enum definition is as follows

enum GENDER {
    male,
    female
}

Its not possible to check if there is a valid input as even a missing input (0) points to a valid index (male). So to handle this, you can create another type in the enum at index zero, which is not supposed to be used for a valid input.

enum GENDER {
    invalid
    male,
    female
}

Now, empty input will match the invalid type so you can just check for that. Your code could be like this in the end


function updateProfile(
    string memory _name,
    GENDER _gender
) public onlyOwner {
    if (bytes(_name).length!=0) {
        patientInfo.name = _name;
    }
    if (_gender!=GENDER.invalid) {
        patientInfo.gender = _gender;
    }
}

Hope this helps. You can read up about the zero value for different data types for further clarification.

1

You can check the string by casting it to bytes then check the length which will determine the parameter value available or not.

function updateProfile(
    string memory _name,
) public onlyOwner {
    if (bytes(_name).length > 0) {
        patientInfo.name = _name;
    }
}

Your Answer

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

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