2

Here is a contract from https://www.etherchain.org/account/0xc18a451d4ef2a85e7bf8404131c5f994a98bdb15#code

What exactly does idx mean or do?

    function enter() {
    if (msg.value < 500 finney) {
        msg.sender.send(msg.value);
        return;
    }

    // add a new participant to array
    uint idx = participants.length;
    participants.length += 1;
    participants[idx].etherAddress = msg.sender;
    participants[idx].amount = msg.value;

    // collect fees and update contract balance
    if (idx != 0) {
        collectedFees += msg.value / 10;
        balance += msg.value;
    } 
    else {
        //code 
    }
  • If the answer helped you, please don't forget to accept it as the correct answer by clicking the tick sign below voting arrows. This applies to all of your questions. – niksmac Mar 22 '16 at 2:43
4

The line uint idx = participants.length; gets the length of the array and participants.length += 1; this one adds 1 to it. Finally participants[idx] adds a new key to the array and assign new participants to it.

An alternative will be a simple push and this #83 might be the reason why the coder avoid push

  • Is idx just the name of the variable? – Vesa Mar 22 '16 at 1:38
  • @Vesa, yes :) uint idx = participants.length; – niksmac Mar 22 '16 at 1:39

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.