Getting the length of public array variable (getter)

0 votes

I am trying to get the length of array from another contact. How?

contract Lottery {
    unint[] public bets;
}

contract CheckLottery {
    function CheckLottery() {
        Lottery.bets.length;
    }
}

Sep 24, 2018 in Blockchain by slayer
• 29,300 points
156 views

1 answer to this question.

0 votes

The calling contract will need the ABI and contract address, which is handled via the state var and constructor below.

pragma solidity ^0.4.8;

contract Lottery {

    uint[] public bets;

    function getBetCount()
        public 
        constant
        returns(uint betCount)
    {
        return bets.length;
    }
}

contract CheckLottery {

    Lottery l;

    function CheckLottery(address lottery) {
        l = Lottery(lottery);
    }

    function checkLottery() 
        public
        constant
        returns(uint count) 
    {
        return l.getBetCount();
    }
}
answered Sep 24, 2018 by digger
• 26,680 points

Related Questions In Blockchain

0 votes
2 answers
+1 vote
1 answer
+4 votes
4 answers

What are the implementations of IBM blockchain?

IBM is an open source community of ...READ MORE

answered Mar 29, 2018 in Blockchain by Christine
• 15,810 points
128 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,810 points
411 views
0 votes
1 answer

Call contract methods with web3 from newly created account

In fact, we can't just send transactions ...READ MORE

answered Sep 25, 2018 in Blockchain by digger
• 26,680 points
2,610 views
0 votes
1 answer
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,030 points
291 views
0 votes
1 answer
0 votes
1 answer