Consider the case where I want to get the elements of a struct via a public mapping from product contract to price contract:
contract Product{
struct prod{
string name;
uint id;
bool status;
}
mapping(uint => prod) public products;
function updateStudent (string n, uint ID) {
products[ID].n = "Temp";
products[ID].status = true;
}
}
At this point, I would like to access the elements of the mapping from a different contract:
contract ProductList {
address productAddr;
string p_name;
uint p_id;
Product p;
function ProductList(address addr) {
productAddr = addr;
p = Product(addr);
}
function readProduct (uint u) returns (bool c) {
p_id = p.products[u].id;
p_name = p.products[u].name;
c = true;
}
}
In the above scenario, I am always getting the error: Indexed expression has to a type, mapping or array (is function…). Any suggestions would be greatly helpful!
Best Answer
Main issue is you're calling a function in the foreign contract, so you use
()
instead of[]
.Changed
string
tobytes32
so contracts can communicate. Also various touchups while I debugged it.bytes32
is more "native" and efficient, so it's generally best to do type conversion in the front-end.I noticed the original concept was "
ListProducts
". Seems to be leading toward an indexed list of the mapped structs, so this might help: Are there well-solved and simple storage patterns for Solidity?Hope it helps.