A Cameron
A Cameron

Reputation: 47

How to use set with a string

I'm using a smart contract to update a value on the blockchain but the set method only accepts integers.

This handleSubmit event sets the new stored value and gets a response to confirm it has been updated.

async handleSubmit(event) {
    alert('A value was submitted: ' + this.state.newValue);
    event.preventDefault();
      // Use web3 to get the user's accounts.
      const { accounts, contract } = this.state;
      await contract.methods.set(this.state.newValue).send({ from: accounts[0] });
      const response = await contract.methods.get().call();
      this.setState({storageValue: response});

This is the smart contract

pragma solidity ^0.5.0;

contract SimpleStorage {
string storedData;


function set(string x) public {
   storedData = x;
  }

  function get() public view returns (string) {
    return storedData;
  }
}

Upvotes: 0

Views: 632

Answers (1)

Suraj Kohli
Suraj Kohli

Reputation: 567

In solidity v0.5.0 it states in the breaking changes list that:

Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables.

Since string is an array in solidity you need to mention the data location for the same.

So your code should look like

function set(string memory x) public {
   storedData = x;
  }

  function get() public view returns (string memory) {
    return storedData;
  }
}

Upvotes: 1

Related Questions