Question:

Member "length" not found or not visible after argument-dependent lookup in string memory

Elijah: 2 weeks ago

I am currently trying to build a simple Process in Solidity, but somehow I get :

Error: Member "length" not found or not visible after argument-dependent lookup in string memory for the function post.

I'd be very thankful if anyone could help!

pragma solidity ^0.4.18;
contract billboard {           
    struct Contributor{             
        string name;                    
        string email;                   
        string message;                
        address addr;                  
    }
    address public owner;           
    string public name;                 
    string public default_name;         
    string public email;                
    string public message;              
    address public addr;                
    uint public numPosts;               
    uint public limit_posts;            
    mapping( uint => Contributor ) public contributors;  
    uint public timestamp;              
    address public winnerAddress;       
    uint public winnerInd;              
    bool public stopped;                

    modifier onlyOwner() {              
    require(msg.sender == owner);
    _;
    } 
    modifier isStopped() {              
    require(!stopped);
    _;
    }

    function billboard(string _title) public {  
        owner = msg.sender;                             
        numPosts = 0;                                   
        stopped = false;                                
        limit_posts = 1002;
    }    
    function post(string _name, string _email, string _message) public payable isStopped {     
        name = _name;                                   
        email = _email;                                
        message = _message;                             
        require(_message.length != 0);                      
        require(limit_posts > numPosts + 1);                
        if(_name.length == 0) {                             
            _name = default_name;
        }
    }
    function hold() public onlyOwner {            
        require(numPosts >= 3);                         
        timestamp = block.timestamp;                
        winnerInd = timestamp % 3;                 
        winnerAddress = contributors[winnerInd];
        if(!winnerAddress.send(this.balance)) {     
                require( false ) ;
        }
    }
    function kill() public onlyOwner {          
        selfdestruct(owner);
    }
}

Answer:
Noah: 2 weeks ago

The error message is pretty clear. You're trying to access a member called length on a variable of type string, but no such member exists.

From https://solidity.readthedocs.io/en/v0.4.23/types.html#arrays:

string is equal to bytes but does not allow length or index access (for now).

To accomplish your goal, you probably want to cast to bytes. From the same documentation:

If you want to access the byte-representation of a string s, use bytes(s).length / bytes(s)[7] = 'x';. Keep in mind that you are accessing the low-level bytes of the UTF-8 representation, and not the individual characters!