0

Setting up a public variable creates a default getter function, eg

`string[] public MyArray = ["E1", "3", "7"];`

Entering a value outside of the valid values of 0, 1, or 2 returns something like this:

call to setter_ex.MyArray errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

If I create a getter function I can add a 'requires' for error checking; eg.

`function getElement(uint _element) public view returns(string memory ArrayString){
    require((_element >= 0) && (_element < MyArray.length),"error invalid entry");
    return MyArray[_element];
}`

This returns the desired error message. call to setter_ex.getElement errored: VM error: revert. revert The transaction has been reverted to the initial state. Reason provided by the contract: "error invalid entry". Debug the transaction to get more information.

Is it possible to do any kind of error checking in conjunction with the default getter to see if the entered value is valid that is similar to the 'requires'?

2
  • Doesn't your approach already answer your question?
    – hrkrshnn
    Feb 13, 2021 at 14:24
  • thanks, kind of, just wondering if there is a way of using the default getter instead of having to code one up - probably not as I could not find one
    – macmiller
    Feb 14, 2021 at 3:48

1 Answer 1

0

My assumption at this point is the default getter function that solidity provides does not allow for additional error checking, which can be added elsewhere in the DAPP if necessary. If one wants the error checking within the function then you would need to not use the default getting and provide your own getter with a require function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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