473,749 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class Inheritance

I am trying to derive a class from an existing class with parameters in the
constructor. Does anyone know why this is happening.

The Code

public class ClassA
{
public ClassA( int Param1 )
{
}
}
public class ClassB : ClassA
{
public ClassB( int Param1 )
{
}
}

I get a "No overload for method 'ClassA' takes '0' arguments" compiler
error. But if i change the class A constructor to as below the error
disappears. I know the first instance is creating the Object class which
takes 0 arguments then I am overriding this with the second constructor with
my parameter. Why do I need to do this or am I missing something else.

public class ClassA
{
public ClassA() : base()
{
}

public ClassA( int Param1 )
{
}
}
Thanks

Bob

Nov 15 '05 #1
3 4619
Bob,

The reason for this is that if you do not explicitly call a constructor,
when you create your constructor, it will call the base class constructor
with no parameters. Because you define only one constructor with
parameters, there is no base class constructor that can be called from B.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bob lazarchik" <bo*****@yahoo. com> wrote in message
news:OY******** ******@TK2MSFTN GP12.phx.gbl...
I am trying to derive a class from an existing class with parameters in the constructor. Does anyone know why this is happening.

The Code

public class ClassA
{
public ClassA( int Param1 )
{
}
}
public class ClassB : ClassA
{
public ClassB( int Param1 )
{
}
}

I get a "No overload for method 'ClassA' takes '0' arguments" compiler
error. But if i change the class A constructor to as below the error
disappears. I know the first instance is creating the Object class which
takes 0 arguments then I am overriding this with the second constructor with my parameter. Why do I need to do this or am I missing something else.

public class ClassA
{
public ClassA() : base()
{
}

public ClassA( int Param1 )
{
}
}
Thanks

Bob

Nov 15 '05 #2
Bob lazarchik <bo*****@yahoo. com> wrote:
I am trying to derive a class from an existing class with parameters in the
constructor. Does anyone know why this is happening.
Yup - see
http://www.pobox.com/~skeet/csharp/constructors.html

Basically you need to change ClassB's constructor to explicitly call
the correct constructor in ClassA:

public ClassB( int Param1 ) : base(Param1)
{
}
I get a "No overload for method 'ClassA' takes '0' arguments" compiler
error. But if i change the class A constructor to as below the error
disappears. I know the first instance is creating the Object class which
takes 0 arguments then I am overriding this with the second constructor with
my parameter.


You can't override constructors - you were overloading the constructor.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
On Fri, 23 Jan 2004 14:32:09 -0500, Bob lazarchik wrote:
I am trying to derive a class from an existing class with parameters in the
constructor. Does anyone know why this is happening. The Code public class ClassA
{
public ClassA( int Param1 )
{
}
}
public class ClassB : ClassA
{
public ClassB( int Param1 )
{
}
} I get a "No overload for method 'ClassA' takes '0' arguments" compiler
error. But if i change the class A constructor to as below the error
disappears. I know the first instance is creating the Object class which
takes 0 arguments then I am overriding this with the second constructor with
my parameter. Why do I need to do this or am I missing something else. public class ClassA
{
public ClassA() : base()
{
} public ClassA( int Param1 )
{
}
}
Thanks Bob


Probably what you are trying to do is this:

public class ClassB : ClassA
{
public ClassB(int Param1) : base(Param1)
{
}
}

which should compile fine.

Tim
--
To email me, make the snot hot.
Nov 15 '05 #4

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

Similar topics

11
3242
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes is out of the question, as there are class Y which only inherits from class A, etc. TIA
6
3195
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
22
5834
by: Marcin Vorbrodt | last post by:
Taken out of C++ In a Nutshell book... Example 7-18: Using an abstract classes as interface specification. struct Runnable { virtual void run() = 0; }; struct Hashable { virtual size_t hash() = 0; };
9
5886
by: mead | last post by:
What kind of classes is qualified as "concrete classes"? When should a member function in a class defined as "pure virtual" and when as "virtual"? Thanks!
4
4029
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best Regards KInd --
166
8644
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
11
2876
by: Darren.Ratcliffe | last post by:
Hi guys Posted what was probably a rather confusing post about this the other day, so I am going to have another go and see if I can make more sense. This sis purely a I've got a base class called animal, and from within animal you can access lots more classes such as feline, canine, reptile and amphibian.....
3
2248
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 {
3
1584
by: Jam | last post by:
Hello All, Your comment is needed on following subject,i define the Class and Inheritance as following,what do you think? Class: Class is a set of objects which shares common states and behaviors,Class can contain subclasses. Inheritance: if two classes are having two common states and behaviors or if a class is derived from another class it is called Inheritance, All's comment is needed,is this defination ok or something more should
3
1668
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
0
8833
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
9256
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
8257
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
6801
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
6079
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
4709
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
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
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.