C
C#2y ago
Nora

✅ 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
realivanjxツ
realivanjxツ2y ago
u need to create a ctor in Librarian like this (im on mobile so cmiiw): public Librarian(): base(arg1, arg2, etc)
Nora
NoraOP2y ago
Unexpected argument list? 😅
Nora
NoraOP2y ago
Nora
NoraOP2y ago
Nora
NoraOP2y ago
realivanjxツ
realivanjxツ2y ago
realivanjxツ
realivanjxツ2y ago
or
realivanjxツ
realivanjxツ2y ago
realivanjxツ
realivanjxツ2y ago
this is the ctor, not class definition (notice the ())
Nora
NoraOP2y ago
It's indeed solved, but I don't quite understand why. Can you explain me the reasons?
realivanjxツ
realivanjxツ2y ago
bc Person only has ctor with arguments u cant instantiate Person without arguments unless u have public Person() { } inside Person
Nora
NoraOP2y ago
What does it have to do with Librarian?
realivanjxツ
realivanjxツ2y ago
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
Nora
NoraOP2y ago
Why can't Librarian directly borrow the constructor from Person? Sorry if my questions are silly
realivanjxツ
realivanjxツ2y ago
wym borrow? librarian doesnt know how to fill person automatically need to explicitly set that
Nora
NoraOP2y ago
I thought that child class would inherit methods and fields, properties from the base class, so I thought it would inherit the constructor too
realivanjxツ
realivanjxツ2y ago
u cant. if u do new Librarian() then how librarian supposed to know what to give to person?
Nora
NoraOP2y ago
Ooh, I get it
realivanjxツ
realivanjxツ2y ago
the values need to be set somewhere for person
Nora
NoraOP2y ago
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
realivanjxツ
realivanjxツ2y ago
just imagine it as calling the Person's constructor first base refer to Person's ctor
realivanjxツ
realivanjxツ2y ago
Nora
NoraOP2y ago
Okay, 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?
realivanjxツ
realivanjxツ2y ago
librarian can have arbitrary parameters doesnt matter just make sure base is called properly
Nora
NoraOP2y ago
Wait, 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?
realivanjxツ
realivanjxツ2y ago
librarian can be known as both librarian and person a person can only be known as a person
Nora
NoraOP2y ago
I see! Is it possible to pass a Librarian object into a method requiring a Person parameter?
public static SayHello(Person personObj)
{
//code
}
public static SayHello(Person personObj)
{
//code
}
Librarian lib = new Librarian("Sarah", 21);
SayHello(lib);
Librarian lib = new Librarian("Sarah", 21);
SayHello(lib);
realivanjxツ
realivanjxツ2y ago
yes
Nora
NoraOP2y ago
Thanks! You've been sooo helpful That's it for now, I won't bother you anymore See you around!
Want results from more Discord servers?
Add your server