Hi @rostyslavdzhohola,
No, it assigns the new person to the mapping people
in the following line:
people[creator] = newPerson;
We don’t change anything in the Person struct. As I said before, we are using the struct like we do a class in JavaScript. Structs and classes are like templates or blueprints from which we can create new object instances to which we assign data on an instance by instance basis. We do that first in the createPerson
function, then we pass newPerson
(the new instance of Person) into insertPerson()
which in turn assigns it to the mapping people
. In the parameter Person memory newPerson
, Person
acts like a data type, in the same way as uint
or string
or address
. So in the same way that we can pass a string data type into a function, as follows:
string memory name
… we can also pass a new instance with the same data structure as the Person struct blueprint, as follows:
Person memory newPerson
The getPerson
function’s ability to retrieve a specific person’s details is based on the mapping. As the mapping has been defined as follows…
mapping(address => Person) private people;
…each new person added will be stored as a key/value pair: the key being an address, and the value being an instance of the Person struct. So you will only be able to “getPerson” by their address, and not by their ID. In contrast, the mapping in the Data Location Assignment has been created to store key/value pairs with keys that have a uint
data type. So, in order to be able to “getPerson” by ID in HelloWorld, you would need to create a new mapping whose keys also have a uint
data type.
I would need to see where exactly in your code you’ve added uint id
, to be able to say for certain why you are getting the first person created each time you call getPerson()
. However, I hope this is enough to clarify things, but do let me know if anything is still unclear, and if you have any more questions.