0

This question already has an answer here:

I am still learning the best way to use mappings and how to access mapped elements. I understand how they work, but I tried to write some simple code to map an ID to a name. It works fine except for the most important part, returning the name. Here is the code:

pragma solidity ^0.4.18;

contract A {
    string public name;

    function A(string _name) public {
        name = _name;
    }

    function getName() public view returns (string) {
        return name;
    }
}

contract handleArray {
    uint public arrIndex;

    mapping(uint => address) testArray;

    function handleArray() public {
        arrIndex = 0;
    }

    function newEntry(address _newA) public {
        testArray[arrIndex] = A(_newA);
        arrIndex++;
    }

    function returnEntry(uint _index) public returns (string) {
        return A(testArray[_index]).getName();
    }
}

So if I leave out returnEntry, I can run everything. I can map new A's to an incremented ID. However, returnEntry does not compile in Remix. I get the error

Return argument type inaccessible dynamic type is not implicitly convertible to expected type (type of first return variable (string memory).

I would appreciate any help. Thanks.

marked as duplicate by user19510, Roman Frolov, Achala Dissanayake, Richard Horrocks, eth Feb 1 '18 at 6:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Thanks to smarx I have figured this one out by looking at the other question he referenced.

The code is now:

pragma solidity ^0.4.18;

    contract A {

        bytes32 name;
        function A(bytes32 _name) public {
            name = _name;
        }

        function getName() public constant returns (bytes32) {
            return name;
        }
    }

    contract handleArray {

        bytes32 public name;
        uint public arrIndex;

        mapping(uint => address) testArray;

        function handleArray() public {
            arrIndex = 0;
        }

        function newEntry(address _newA) public {
            testArray[arrIndex] = A(_newA);
            arrIndex++;
        }

        function returnEntry(uint _index) public returns (string) {
            bytes32 input;
            string memory output;
            A a = A(testArray[_index]);

            // get name in bytes32
            input = a.getName();

            bytes memory bytesArray = new bytes(32);
            for (uint256 i; i < 32; i++) {
                bytesArray[i] = input[i];
            }

            // convert ASCII to String
            output = string(bytesArray);

            return string(output);
        }
    }

Thanks again!

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