473,704 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Base class does not contain a constructor that takes '0' argument

I have

class BaseClass
{
public BaseClass(strin g s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set;}
public string S2 { get; set;}
}

class SubClass: BaseClass
{
public SubClass(int int1, int int2)
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}

VS2008 intellisense underlines the constructor of SubClass and says:

Error 1 'ConsoleApplica tion1.BaseClass ' does not contain a constructor
that takes '0' arguments.

Why? Any hint is highly appreciated.
Nov 6 '08 #1
6 47669
Author wrote:
I have

class BaseClass
{
public BaseClass(strin g s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set;}
public string S2 { get; set;}
}

class SubClass: BaseClass
{
public SubClass(int int1, int int2)
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}

VS2008 intellisense underlines the constructor of SubClass and says:

Error 1 'ConsoleApplica tion1.BaseClass ' does not contain a constructor
that takes '0' arguments.

Why? Any hint is highly appreciated.
C# enforces that construction of the base class must be properly completed
before the derived class constructor is executed. Because your derived
constructor doesn't explicitly call a base class constructor, the compiler
automatically inserts a call to the constructor without arguments. But
there's no such constructor, so it fails.

To fix this, you must make a constructor available for the derived class to
call. In this case, initializing the properties does not seem to be
mandatory (since users can pass null for both, and even unset them later) so
there's no problem making a public parameterless constructor available. In
the general case, you could make a protected constructor available
specifically for derived classes to initialize the base state with.

--
J.
Nov 6 '08 #2


Author wrote:
I have

class BaseClass
{
public BaseClass(strin g s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set;}
public string S2 { get; set;}
}

class SubClass: BaseClass
{
public SubClass(int int1, int int2)
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}

VS2008 intellisense underlines the constructor of SubClass and says:

Error 1 'ConsoleApplica tion1.BaseClass ' does not contain a constructor
that takes '0' arguments.

Why? Any hint is highly appreciated.
Let's say you create an object of SubClass like this
SubClass sc = new SubClass(1,2);

Now, when subclass' object is created, it's base class object is also
created internally. So, it's constructor also must be invoked. Now, the
derived class is not explicitly specifying which constructor to use for
the base class, in which case the compiler searches for a constructor
with 0 parameters.

So, to solve this error, you can either add a default constructor to
BaseClass or do something like this

class SubClass : BaseClass
{
public SubClass(int int1, int int2):base(""," ")
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}
Nov 6 '08 #3
Author,

The compiler basically assumes this when you write your constructor:

class SubClass : BaseClass
{
public SubClass(int int1, int int2) : base()
{

Since you have defined only one constructor in BaseClass and it takes
arguments, that's where the error comes from. Basically, you need to
explicitly call the constructor on the base class.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Author" <gn********@gma il.comwrote in message
news:94******** *************** ***********@h23 g2000prf.google groups.com...
>I have

class BaseClass
{
public BaseClass(strin g s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set;}
public string S2 { get; set;}
}

class SubClass: BaseClass
{
public SubClass(int int1, int int2)
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}

VS2008 intellisense underlines the constructor of SubClass and says:

Error 1 'ConsoleApplica tion1.BaseClass ' does not contain a constructor
that takes '0' arguments.

Why? Any hint is highly appreciated.

Nov 6 '08 #4
Many thanks to both of you.

Actually, I did notice that if I call the base class's contructor
using :base(blah blah), it error goes away.

So, the conclusion is: either the base class's constructor must be
explicitly called using base() or a parameterless constructor must be
provided for the base class. Correct?
Nov 6 '08 #5
Author wrote:
So, the conclusion is: either the base class's constructor must be
explicitly called using base() or a parameterless constructor must be
provided for the base class. Correct?
Well, *a* base class constructor must be called (you can have any number of
them) and if you don't call one explicitly, there indeed must be a
parameterless one to call implicitly.

--
J.
Nov 6 '08 #6
On Nov 6, 2:38*pm, Jeroen Mostert <jmost...@xs4al l.nlwrote:
Author wrote:
So, the conclusion is: either the base class's constructor must be
explicitly called using base() or a parameterless constructor must be
provided for the base class. Correct?

Well, *a* base class constructor must be called (you can have any number of
them) and if you don't call one explicitly, there indeed must be a
parameterless one to call implicitly.

--
J.
Perfecto! Gotcha. Thx.
Nov 6 '08 #7

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

Similar topics

10
3321
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
10
1807
by: Andrzej Kaczmarczyk | last post by:
Hi C# gurus :) I have a conversion problem. I have a class that I can't change - it's a DataColumn class; I have a wrapper class around it. public MyDataColumn : DataColumn {
1
2251
by: John | last post by:
Hi, Maybe someone can help me with the following: "The first task by any derived class constructor is to call it’s direct or indirect base class constructor implicitly or explicitly", reads the statement. To test this, I added messageboxes in all constructors indicating which constructor is called. There are four constructors which could be called: 1. Base class empty constructor 2. Base class 2-Par. constructor
1
1470
by: Martijn Mulder | last post by:
Documentation suggests that I should make a call to the base-class method for some methods. When I try to do so for the class below, I get wrong results. What is the correct way to make a call to the base class for an event like this? //class Form class Form:System.Windows.Forms.Form {
3
2247
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
26
5364
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
1
2522
by: Belebele | last post by:
I would like to have the compiler bind a Base class const reference to a temporary object of a Derived class when calling another class constructor. Please see the code below: class Base {}; class Derived: public Base {}; class Foo { public:
12
1914
by: siddhu | last post by:
Dear experts, #include <stdio.h> class Base { }; class Der1:public Base {
4
2942
by: softwaredoug | last post by:
Here is some test code I've been attempting to compile (Visual Studio 2003) test.h: class Base { protected: Base() {} public:
0
8689
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,...
1
9030
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
8979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7893
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
6605
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
5933
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4439
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...
1
3137
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
2
2486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.