0

I wrote a function that takes three arguments and assigns the data to 3 public variables. This is the code in .sol file

uint[] public rule1;
uint cond1;
string name1;

function setRule (string _name, uint _cond, uint[] _rule) public {

        for (uint i = 0; i < _rule.length; i++) {
            rule1[i] = _rule[i];
        }
        name1 = _name;
        cond1 = _cond;
    }

After compiling and running the contract in geth CLI. I am checking the following commands.

token.rule1[0]

returns 0

token.setRule("my rule", 1, [1,2,3])

Tx is mined

token.rule1(0)

returns 0

token.name1

returns ""

token.rule1[1]

returns 0

I want to assign the values passed in arguments to the public variables. How to do it?

3

Presumably the transaction fails, right? rule1 is of length 0, but you're trying to write to various indexes >= 0.

Try this instead:

for (uint i = 0; i < _rule.length; i++) {
    rule1.push(_rule[i]);
}

push will extend the length of the array and add the item at the (new) end of the array.

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.