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,170 points
50 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,550 points

Related Questions In Blockchain

+1 vote
1 answer
+4 votes
3 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,790 points
63 views
+1 vote
2 answers

Is there any relation between the number of transactions processes and the miners?

the direct answer to your query is ...READ MORE

answered Jun 19, 2018 in Blockchain by Perry
• 17,020 points
49 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,790 points
170 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,550 points
1,017 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
• 67,660 points
104 views
0 votes
1 answer
0 votes
1 answer