Ethereum Asked by Venkatesh Muthyala on December 11, 2021
pragma solidity ^0.4.23;
contract tracking {
struct data {
uint ownernumber;
uint id;
}
uint value;
mapping (uint256 => data) public datamatching;
function storedata (uint _ownernumber, uint _id) public {
var persondata = datamatching[value];
persondata .ownernumber = _ownernumber;
persondata. id = _id;
}
function getData(uint256 userId) returns (uint){
}
}
1) I wrote a function to store the values in my solidity code
2) Now in getData function, I want the user to enter the ownernumber
and get all the details matching to the ownernumber
is it possible please help me
Common errors:
Use of the “var” keyword is disallowed. Data location must be
“storage” or “memory” for variable, but none was given
A sample program:
pragma solidity ^0.5.2;
contract bank{
struct Person{
uint id;
bytes32 name;
}
mapping(address => Person) public p;
function getDetails(address a) public view returns(uint ,bytes32){
Person storage person = p[a];
return(person.id , person.name);
}
function setDetails(address a,uint id , bytes32 name) public{
Person storage person = p[a];
person.id = id;
person.name = name;
}
}
Note: Now, if you use var keyword like var person = p[a];
, you will get below SyntaxError:
SyntaxError: Use of the "var" keyword is disallowed. Use explicit declaration `struct bank.Person storage pointer person = ...´ instead.
If you use Person person = p[a]; , then you will get TypeError :
TypeError: Data location must be "storage" or "memory" for variable, but none was given. Person person = p[a];
So better Option is to use storage
keyword along with Struct name like below:
Person storage person = p[a];
Answered by Vishwa Ratna on December 11, 2021
It is not possible to retrieve data
by ownernumber
as there is no way to iterate mappings unless you know the keys.
You can store all the all the keys of the mappings in an array so that you can iterate mappings and retrieve data for specific ownernumber
. The time complexity of this approach will be O(N).
I am assuming there is one to many relationship among owner and data. That is one owner
is associated with many data
. If it is so then you can reduce the time complexity of retrieving data by owner to O(1). Refer the folowing blog for more details -
I think you need to pass value
as an argument to storedata
function. Let me know if any further details are required.
Hopr it helps.
Answered by Soham Lawar on December 11, 2021
You have two options to do this.
You can return multiple variables from a function, like this:
pragma solidity ^0.4.23;
contract tracking {
struct data {
uint ownernumber;
uint id;
}
uint value;
mapping (uint256 => data) public datamatching;
function storedata (uint _ownernumber, uint _id) public {
var persondata = datamatching[value];
persondata .ownernumber = _ownernumber;
persondata. id = _id;
}
function getData(uint256 userId) returns (uint, uint){
return (datamatching[userId].ownernumber, datamatching[userId].id);
}
}
Alternatively you can use the experimental v2 abi encoder that allows returning structs from functions, like this:
pragma solidity ^0.4.23;
pragma experimental ABIEncoderV2;
contract tracking {
struct data {
uint ownernumber;
uint id;
}
uint value;
mapping (uint256 => data) public datamatching;
function storedata (uint _ownernumber, uint _id) public {
var persondata = datamatching[value];
persondata .ownernumber = _ownernumber;
persondata. id = _id;
}
function getData(uint256 userId) returns (data){
return datamatching[userId];
}
}
Answered by natewelch_ on December 11, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP