C
C#7mo ago
Finnland

I cant figure out constructor inheritance

how do i inherit classes, including their constructors? no matter what i try i get a wide variety of different errors. i have tried just:
public class SubClass : Superclass {

}
public class SubClass : Superclass {

}
but then i get the error
'SuperClass' does not contain a constructor that takes 0 arguments
'SuperClass' does not contain a constructor that takes 0 arguments
so then i tried adding
public class SubClass : Superclass {
public SubClass(arg) : base(arg)
{

}
}
public class SubClass : Superclass {
public SubClass(arg) : base(arg)
{

}
}
but that gives me an error that i need a method body. How do i inherit the constructors in addition to the rest of the class?
5 Replies
Jimmacle
Jimmacle7mo ago
your example looks mostly correct, are you missing a data type for your subclass constructor arguments in your actual code?
Finnland
Finnland7mo ago
ohhh yes thats it thank you i was so confused by the vsc errors (which didnt say anything like that) that i completely missed it
Odd_Dev_404
Odd_Dev_4047mo ago
@SyncRооt - I have a question. In the first code it has just inherited from the base class. public class SubClass : Superclass { } I'm bit confused why it's giving an error as 'SuperClass' does not contain a constructor that takes 0 arguments Any idea?
Odd_Dev_404
Odd_Dev_4047mo ago
@SyncRооt - Got it thanks! I was aware about that "No constructor, so the compiler places an implicit public empty constructor" But wasn't aware constructor inheritance is not supported within C# at this moment until you mentioned that. In this link's first example - https://www.geeksforgeeks.org/c-sharp-inheritance-in-constructors/ isn't an example related to constructor inheritance?
GeeksforGeeks
C# | Inheritance in Constructors - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Odd_Dev_404
Odd_Dev_4047mo ago
@SyncRооt - Now got it. Thanks again. Although it's almost similar the wordings "inheritance and constructors" and "constructor inheritance", thos two are different concepts right?
Want results from more Discord servers?
Add your server
More Posts
✅ Is it possible to simplify this litedb query?All I want is to get total count, count of not delivered, not read, not confirmed. Actually I don't Scaffolding converting sql DateTime from sqlite to byte[], how can I keep it as long / C# DateTimeQuestion: Using Entity Framework to scaffold a sqlite database, its converting datetime to byte arrathe type "src" does't exist in the type 'minecraftDevelopperhello I am trying to make a personalized button in order to subsequently integrate a translation sysConsole Crasheswhen I would type after the 4th line and press enter the console crashes idk whyA way to store references or pointers to objects in arrays/lists?Is there a way to do the following code, which is written in c++, in c#? Assume those variables cannI can't figure out how to fix this error for my input file```cs if( nextLine != null) { string[] nextItem = nextLine.Split(new char[] { ' ', '\t' }, StC# Change Folder IconI want to be able to create a very basic program that will be able to change the icon of a folder to+1 value to database item via OleDB?Program used: Visual Studio (C# Winforms) Hello there!! Just a general question I really needed forI Dont know what this error means and it isnt an issue the guy in the video im watching is having.I am making a 2d platformer, and am trying to get the movement working, but I'm getting an error thaBuild looking for project as a package name.This is a but strange, but I'm following build instructions for https://github.com/AzureAD/microsoft