Hey @wpcrypto,
Well done for working out the correction you needed yourself
Person[] public people;
Here, we are defining our people
array variable with the Person
Data Type. If we want to store instances created from a struct in an array, the Data Type we define it with is the name of the struct (not the keyword struct
). A similar thing happens in mappings to define the Data Types of the key and the data mapped to it, although the mapping definition does also open with the keyword mapping
. This may be confusing at first, because if, for example, we were storing unsigned integers (a primitive data type) we would define the Data Type as uint
:
uint[] public numbers;
We use a capital āPā because this is the convention for naming structs, but you will notice that it does also work if you use a lower case āpā for the struct name i.e.
struct person { ... } // 'P' changed to 'p' here
person[] public people // instead of changing 'p' to 'P' here
The important thing to note here is that, your original error was one of consistency, rather than not using a capital letter. Although, you are right that the best way to fix this inconsistency is to use capital letters (according to convention).
Note that if you use your alternative createPerson function code, ā¦
ā¦ then you also need to make the same change, because you are pushing a new instance of the Person
struct to the people array (i.e. itās the Person
not the person
struct). I also hope you realised that you need to correct your spelling of length
, and also end the statement with a semi-colon, in order for it to work
Iām sure you already realise, but if you use the longer method of creating a new instance (by defining the local variable newPerson
, then you also need to add the following line in order to push it to the array:
people.push(newPerson);
Sorry, if Iām just explaining what youāve already worked out yourself. But I think your original problem raises an interesting issue, and so I think itās good to include a clear explanation here in the forum for other students who are just starting to learn Solidity, as well.