1

I have made a contract with a struct, Stock. I have an array with two such Stocks. They can be initialized fine, but when I try to change the value of a variable nothing happens. Also, the same function cannot change the value of a simple String variable.

Here's the code:

contract Company{

struct Stock{
    address owner;
    string bitcoinAddress;
}

string public companyName;
Stock[2] public owners;
string public test;

function Company(string _companyName, address[2] addresses){
    companyName = _companyName;
    for(uint8 x=0; x<owners.length; x++){
        owners[x] = Stock(addresses[x], "");
    }
 }

function changeStockOwner(uint8 _number, address _newAddress) constant returns(string){
    if(msg.sender == owners[_number].owner){
        owners[_number].owner = _newAddress;
        test = "hello";
        return "well";
    }
}
}

So, when I call company.changeStockOwner(0, personal.listAccounts[1]), it returns "well", so I guess it can get pass the if-statement. But then, nothing changes.

I thought there might be some difficulty changing the value of a struct in an array, so I added test just to see if I could change that, but that won't change either. I've also tried setting web3.eth.defaultAccount to the same same owners[0].owner (just to see if it would work).

I used the online compiler at ethereum.github.io.

I have no idea what I'm doing wrong. It seems just obvious that it should work, but why doesn't it?

0

changeStockOwner() is marked constant, so it's unable to change the blockchain.

I removed the initialization loop from the constructor and added an fudge to the validity check. It let's the contract be initialized with null args in the constructor and facilitates confirmation of the set/get process.

contract Company{

    struct Stock{
        address owner;
        string bitcoinAddress;
    }

    bytes32 public companyName;
    Stock[2] public owners;
    string public test;

    function Company(bytes32 _companyName, address[2] addresses){
        companyName = _companyName;
        // arrays are initialized to 0
     }

    function changeStockOwner(uint8 ownerNumber, string newAddress) returns(bool success){
        // little fudge for testing
        if(msg.sender != owners[ownerNumber].owner && owners[ownerNumber].owner != 0) throw;
        owners[ownerNumber].bitcoinAddress = newAddress;
        return true;
    }
}

Hope it helps.

  • Oh, haha, so it was just one little word, and now it works! Thank you so much! Hm, why not initialize the stockArray, though? – Carl Jakobsson Feb 28 '17 at 0:41
  • Yip. No worries. Couple reasons for not initializing. First is it just doesn't add anything. You get zero and zero-length initial values without doing that. Second, more important reason is the gas cost would increase with list size, and that would mean the function won't run at all above a certain scale. The block gas limit means it's not possible to run an excessively costly transaction even if one is willing to pay for the gas. In case it isn't clear, every iteration over the loop would burn a little gas. Concern is it would add up. – Rob Hitchens Feb 28 '17 at 0:46
  • Ah, I see, you have to economize all code run on ethereum, both because of gas cost and too long codes won't fit in a block. – Carl Jakobsson Feb 28 '17 at 11:20
  • That's right. I later realized it's not a huge concern in this case because limit 2 is baked in. :-) – Rob Hitchens Feb 28 '17 at 13:17

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.