Welcome to the thread about Inheritance & Visibility in Ethereum. Here you can discuss everything about this chapter.
For me The Internal functions are hard to understand completly! but if i understood a little bit, can i continue? THX
http://solidity.readthedocs.io/en/develop/contracts.html#visibility-and-getters
If I have understood :
visibility = who will be able to access our functions ?
public ( by default): anyone can access to the contract , outside and within the contract
private: only from the contract with this level of visibility ( not even for the children contract)
internal: only within the contract and by its children contract
external: only from outside the contract
Thatâs correct. Good job!
âKennel is Dogâ doesnât feel right at all. Kennel is composed of Dogs in real life.
âDog is Animalâ would be better in order to avoid confusion about inheritance and composition.
Thatâs a good point. That would have been a better example of a real-life inheritance situation. If we update the course in the future I will take this into account. Thank you!
Hey Filip, thanks for answering my last question in a timely manner⌠I have another (of course)âŚ
I have been thinking that the use of dogToOwner mapping may not be as clear to read or understand for all the other developers who will be updating my code in the future [smiling]. I think a better and clearer way to code this dogToOwner mapping would be to just add owner as a property in the dog struct and skip the mapping completely. What do you think?
Good question. The issue with not having a mapping at all is that it gets way more difficult to find a specific dog that is owned by a specific owner. If you only have the owner as a property in the struct, you would have to look at every single dog in order to find which is yours. This is very costly and slow. Instead we can use mappings, either dogToOwner or OwnerToDog, to find a dog or an owner very quickly. I hope this answered your questions. Or maybe you had thought about a clever solution?
Thanks for the response @filip ⌠No clever Idea yet⌠but I am starting to think about how I could use mapping and msg.sender in a way that would help people who have lost their crypto private key. One of my goals for taking this course was to come up with an idea for a dapp. Maybe this is one⌠weâll see.
Iâm sure youâll write lots of them, Dapps that is.
you should make a summary after each topic
i have a question about transferDog. is it correct that you can only transfer the last dog that was added to the dogs array? so lets say i run the code, add three dogs, then use the transferDog function, this will only transfer the last dog added correct? so the way the function has been made seems a bit strange to me
Not really. The dogs are not stored in an array. They are stored in a mapping. So every owner points to a dog. What is true however is that we can only store one dog per owner.
Hi Filip, the dogContract code in the link does not compile properlyâŚ
the fix is to remove line 17: addedDog(owner, _name, id);
and to remove the âinternalâ keyword from the addDog function
Cheers!
Hi all,
is there a reason why âmappingsâ are used to link the owner to the Dog, rather than say add an âownerâ field to the âstructâ and put the msg.sender address there?
Thanks, Mark.
Another questionâŚ
when I make the DogContract âaddDogâ function internal, and I compile, the addDog function disappears. It still works on the Kennel Contract when I add in the addKennelDog function though, but Iâm wondering why it cant been seen from within the DogContract itself?
Thanks, Mark.
Thank you! I have fixed it now. The internal keyword should still be present. But the addedDog event was a copy/paste error on my part.
You would still need to save the struct somewhere. Maybe you where thinking of storing the struct in an array instead of a mapping? That is also possible. But it wouldnât be very effective when we are trying to find a certain dog. We would have to search through the entire array. With a mapping we can very effectively find a dog that belongs to a certain owner.
Thatâs because the function is no longer public. The standard visibility is public, meaning it can be executed from anywhere, including from remix. When we put the internal visibility on a function, we can only call the function from the contract itself or the derived contract.
Hi filip,
I am following your course above. Now, stuck at Import files message"Unable to import âbrowser/DogContract.solâ: File not found". could you please help me out.
Below are the code copied from you:
pragma solidity^0.4.0; //Unable to import âbrowser/DogContract.solâ: File not found
import â./DogContract1.solâ;
contract Kennel is DogContract1 {
function transferDog(address _newOwner) returns (uint) {
address owner = msg.sender;
uint dogId = ownerToDog[owner];
delete(ownerToDog[owner]);
ownerToDog[_newOwner] = dogId;
} //at this stage the auto compiler should by ok as in your tutorial but not my Remix
function addKennelDog(string _name, uint _age) {
addDog(_name, _age);
}
}
contract DogContract1{
struct Dog{
string name;
uint age;
}
Dog[] dog;
mapping (address =>uint) ownerToDog;
function Doglist (string _name, uint _age){
address owner = msg.sender;
uint id =dog.push(Dog(_name, _age)); // for mapping add "uint id = ".
ownerToDog[owner] = id;
}
function setDogList () returns (string) { //uint in setDogList() is replaced by codes below
address owner = msg.sender;
uint id = ownerToDog[owner];
return dog [id-1].name;
}
}