473,796 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inheriting from a class with constructors

What is the proper way of defining a class that inherits from a base class
that has no constructor with 0 arguments?

The following code generates compiler error CS1501, "No overload for method
'BaseClass' takes '0' arguments".

class BaseClass
{
public BaseClass(doubl e x)
{
}
}

class TestClass : BaseClass
{
}

Inserting the constructor

public TestClass(doubl e x)
{
}

into TestClass still generates the same error.

Nov 17 '05 #1
3 1676
I think you want:

class TestClass : BaseClass {
public TestClass(doubl e x) : base(x)
{
// whatever
}
}

This is "constructo r chaining".

--
I may not always have the best answers, but at least I'm not posting
questions in the wrong newsgroups...
"G. Purby" <GP****@discuss ions.microsoft. com> wrote in message
news:E9******** *************** ***********@mic rosoft.com...
What is the proper way of defining a class that inherits from a base class
that has no constructor with 0 arguments?

The following code generates compiler error CS1501, "No overload for
method
'BaseClass' takes '0' arguments".

class BaseClass
{
public BaseClass(doubl e x)
{
}
}

class TestClass : BaseClass
{
}

Inserting the constructor

public TestClass(doubl e x)
{
}

into TestClass still generates the same error.

Nov 17 '05 #2
It goes something like this:

If you define a class without a constructor, then the compiler will
generate an implicit constructor that takes no arguments.

So, if you write:
class A
{
};

The compiler will generate:
class A
{
public A()
{
// does nothing.
}
}
However, if you define a class with a constructor, then the compiler
will NOT generate a default constructor for you.

So if you type:
class B
{
public B( double d )
{
// more code goes here.
}
}

The compiler will generate exactly that.

Suppose now that you want to inherit from B:

class C : B
{
public C()
{
// yet more code goes here.
}
}

Then the constructor for class C MUST call a constructor for class B.
Since you have not explicitly specified what constructir to call for
class B, the compiler will attempt to call a parametless constructor.

So, the compiler will TRY to generate:

class C : B
{
public C()
{
B();
}
}

But since you have already defined a constructor for class B, one that
DOES take a parameter, the compiler will not generate a default
parameterless constructor for B, and as such, the compilation will
fail.

To remedy this, either explicitly define a paramaterless constructor
for B, as follows:

class B
{
public B( double d )
{
// more code goes here.
}

public B()
{
// whatever.
}
}

Or, modify class C to explicitly call the constructor in class class B,
as follows:

class C : B
{
public C() : base(0.0)
{
// yet more code goes here.
}
}

Nov 17 '05 #3
Thank you, Mohammad. The way you have shown of defining a constructor with a
": base()" clause is exactly the feature I need. Problem solved.

What had originally confused me was that the compiler complained about a
wrong number of arguments, not a failure to invoke the base constructor. You
have made clear why it would do that.
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2527
by: sureshsundar007 | last post by:
Hi all, I'm trying to inherit the SimpleWorkerRequest class but cant able to do so. I'm getting an error called "System.Web.Hosting.SimpleWorkerRequest.SimpleWorkerRequest()' is inaccessible due to its protection level" I tried my dervived class with public,internal and private
2
6924
by: SpotNet | last post by:
Hi Newsgroup, Reconstructing my common dialog assembly using C# 2.0 .NET Framework 2.0. I have the following; public class FileDialogBase: System.Windows.Forms.FileDialog { //No constructor seem to do me any justice.... }
2
1625
by: Jim Heavey | last post by:
I amd playing around with inheritance a little in VB.Net If I create a new class which inherits from ListViewItem and I am only wanting to override the ToString Method. In this situation, If I only override the "ToString" method, I find that I only have a single constructor. Do I have to clone all of the constructor methods and then execute the MyBase.New(....) methods for each of those constructors? If I am inheriting all methods and...
4
1977
by: Brad Langhorst | last post by:
Books online says that constructors are not inherited in vb.net (grrr - why not?) I have a series of classes that share the same constructors and find myself cutting and pasting when i make changes to them ... a sure sign of something badly wrong. How do you all deal with sharing this code? I can't seem to find any sort of #include feature Is there some option?
4
1199
by: elziko | last post by:
I would like to do the following: Create a class that inherits from a usercontrol. Then add a control to the designer Call this new class ClassA. Many controls I need will start off like this so I'd like to create all controls form now on from this base control. So, for example, I inherit from this base control and add the further controls required to form this part of my UI. Call this ClassB
2
4857
by: TCook | last post by:
Hello All, First I tried to inherit from the TabPageCollection class but received the following error message: No overload for method 'TabPageCollection' takes '0' arguments I also tried implementing a custom collection and the tabpages show up during design time in the properties but the tabs are never visible during design or runtime. The collection contains classes inherited from a tabpage
2
2246
by: Christof Warlich | last post by:
Hi, I'd like to define a class that should behave as much as posible like std::string, but that has some small additional property: class ExtendedString: public std::string { public: void someExtendedFunctionality(void) {} };
0
1046
by: Jack Jackson | last post by:
There is a class (over which I have no control) that has no public constructors - new instances are created via a Static function. I would like to modify the behavior of one of the methods of this class, but I can't inherit from it because it has no public constructors. Is there any other way to modify the behavior of one of the class's methods?
4
1295
by: Thomas Wittek | last post by:
Hi! I'm relatively new to Python, so maybe there is an obvious answer to my question, that I just didn't find, yet. I've got quite some classes (from a data model mapped with SQL-Alchemy) that can be instatiated using kwargs for the attribute values. Example: class User(object): def __init__(self, name=None):
4
1700
by: AalaarDB | last post by:
struct base { int x, y, z; base() {x = 0; y = 0; z = 0;}; base(int x1, int y1, int z1) {x = x1; y = y1; z = z1;}; }; struct intermediate1 : public virtual base {}; struct intermediate2 : public virtual base
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.