0

I am calling the function getPrice() with arguments tokenId but it returns 0, while i have assigned price = 1 ether, where is the problem in this code?

pragma solidity ^0.6.7;

import "./S5Implement.sol";

  contract newToken is S5Implement {

      uint256 private price = 1 ether;

      mapping (uint256 => uint256) private priceMapping;

      function getPrice(uint256 _tokenId) public returns(uint256) {

           price = priceMapping[_tokenId];

      }
  }
  • You are not actually returning any value – Lauri Peltonen May 29 at 8:24
  • i have checked it with return too but same it returns 0, while i the price = 1 ether – sultan May 29 at 9:42
  • Well you are overwriting the value of price - it's probably 0. – Lauri Peltonen May 29 at 9:44
  • pragma solidity ^0.6.7; import "./S5Implement.sol"; contract newToken is S5Implement { uint256 private price = 1 ether; mapping (uint256 => uint256) private priceMapping; function getPrice(uint256 _tokenId) public returns(uint256) { price = priceMapping[_tokenId]; return price; } } this is the code – sultan May 29 at 9:45
  • please correct it if it is wrong – sultan May 29 at 9:46
0

You need to return value from mapping instead of overriding. Read documentation here.

pragma solidity ^0.6.7;

import "./S5Implement.sol";

contract newToken is S5Implement {

      uint256 private price = 1 ether;

      mapping (uint256 => uint256) private priceMapping;

      function getPrice(uint256 _tokenId) public view returns(uint256) {
           return priceMapping[_tokenId];
      }
  }

| improve this answer | |

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.