3

Is there a way to get length of an array in an other contract without a getter?

pragma solidity ^0.4.11;

contract MyContractA {
    uint[] public myArray;

    function MyContractA() {
        myArray.length = 1;
    }

}

contract MyContractB {

    function test() returns (uint ret) {
        address instanceAddress = new MyContractA();
        MyContractA instance = MyContractA(instanceAddress);
        // works:
        return instance.myArray(0); 
        //
        // doesn't work: 
        //
        //  TypeError: Member "length" not found or not visible after argument-dependent lookup in function (uint256) constant external returns (uint256)
        // return instance.myArray.length;

        // TypeError: Member "length" not found or not visible after argument-dependent lookup in function (uint256) constant external returns (uint256)
        // return instance.myArray.length();

        //   TypeError: Wrong argument count for function call: 0 arguments given but expected 1.
        //return instance.myArray().length;
    }
}
7

No. You need to present a function that will return the array length as a uint.

Something like

function getCount() public constant returns(uint count) {
    return array.length;
}

It's a pretty common requirement. These storage patterns might save you some time working out how to do things within the constraints we're dealing with: Are there well-solved and simple storage patterns for Solidity?

Hope it helps.

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.