4

Given the below code, can I use a variable defined in a modifier (ie _organization), in my function? It's not compiling now but wondering if there is any way to do this?

modifier onlyCEOOrCOO {
     Organization memory _organization = organizations[addressToOrgId[msg.sender]];
     require(
       msg.sender == _organization.cooAddress ||
       msg.sender == _organization.ceoAddress
     );
     _;
   }

function something () external onlyCEOOrCOO {
    // is _organization available here? 
    // If I had used storage instead of memory can I update _organization here?
}
  • I don't think this is possible, no. – user19510 Aug 14 '18 at 21:05
1

Let's test it out!

pragma solidity ^0.4.24;

contract ModifierTest {

    address public owner;

    struct myStruct {
        uint256 fancyVar;
    }

    constructor() public {
        owner = msg.sender;
    }

    modifier myFancyModifier {
        uint16 a = 314;
        myStruct memory b;
        b.fancyVar = 159;
        _;
    }

    function myFunction() external myFancyModifier {
        uint16 c = a * 2;
        uint16 d = b.fancyVar * 3;
    }
}

The contract above yields the following errors when running truffle compile:

DeclarationError: Undeclared identifier. uint16 c = a * 2;

DeclarationError: Undeclared identifier. uint16 d = b.fancyVar * 3;

For both storage and memory, you cannot cross-use modifier variables in functions.

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.