✅ Error CS7036 - while trying to inherit a class
I'm building a small Library Management application. I'm using the Person class to manage the relevant people, including the Librarian, Reader and Regular Reader. However, when I attempt to let the Librarian class inherit the Person class, an error showed up.
It probably has something to do with my Person() constructor, but I'm not too sure why.
I've tried adding parameters at the class name (how silly and desperate!), but it of course didn't work.
I've tried adding a Librarian() constructor that has all the required parameters, but the same error is still there (fortunately without new ones?).
I'd appreciate all the help!
29 Replies
u need to create a ctor in Librarian like this (im on mobile so cmiiw):
public Librarian(): base(arg1, arg2, etc)
Unexpected argument list? 😅
or
this is the ctor, not class definition (notice the
()
)It's indeed solved, but I don't quite understand why. Can you explain me the reasons?
bc Person only has ctor with arguments
u cant instantiate Person without arguments
unless u have
public Person() { }
inside PersonWhat does it have to do with Librarian?
librarian is based on Person
so when librarian is instantiated it has to instantiate person too
but u cant instantiate person without arguments
so u somehow need to fill person's arguments with librarian
Why can't Librarian directly borrow the constructor from Person?
Sorry if my questions are silly
wym borrow?
librarian doesnt know how to fill person automatically
need to explicitly set that
I thought that child class would inherit methods and fields, properties from the base class, so I thought it would inherit the constructor too
u cant. if u do
new Librarian()
then how librarian supposed to know what to give to person?Ooh, I get it
the values need to be set somewhere for person
Can you explain me the syntax, too? Because here it looks as though the Librarian constructor is "inheriting" the Person constructor, I don't quite get that
just imagine it as calling the Person's constructor first
base
refer to Person's ctorOkay, so we throw the information we got from the Lib's cter to Person's to instantiate a Person, then we return to the Lib cter to implement whatever it has, is that correct?
librarian can have arbitrary parameters doesnt matter
just make sure
base
is called properlyWait, wait, just a knowledge check for me, so is the Librarian known by the compiler as a "Person" instead of a Librarian? Because we instantiated a Person, then built upon that foundation... right?
librarian can be known as both librarian and person
a person can only be known as a person
I see!
Is it possible to pass a Librarian object into a method requiring a Person parameter?
yes
Thanks! You've been sooo helpful
That's it for now, I won't bother you anymore
See you around!