473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

a question about inheritance in .net

hey

..net 2.0

Below I declare 2 classes

******** class A ***********
public class A{
protected int _id;
public int Id
{
get { return _id; }
set { _id = value }
}

public A (int id)
{
this.Id = id;
}
}

******** class B ***********
public class B : A
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value }
}

public B (int id, string name)
{
this.Id = id;
this.Name = name;
}
}

In my .net code I've defined 2 similar classes. When I build the code I get
an compile error on the constructor of class B, it gives this error messsage
"No overload for method A takes 0 arguments"..
what am I missing here??

Nov 26 '07 #1
2 1110
Jeff wrote:
hey

.net 2.0

Below I declare 2 classes

******** class A ***********
public class A{
protected int _id;
public int Id
{
get { return _id; }
set { _id = value }
}

public A (int id)
{
this.Id = id;
}
}

******** class B ***********
public class B : A
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value }
}

public B (int id, string name)
{
this.Id = id;
this.Name = name;
}
}

In my .net code I've defined 2 similar classes. When I build the code I get
an compile error on the constructor of class B, it gives this error messsage
"No overload for method A takes 0 arguments"..
what am I missing here??
If you do not create a constructor for your class, C# will create one
for you automatically. Since you have created your own constructor in
A, you do not get the C# created default constructor.

You can either add your own default constructor to A - Ex:

public A()
{
}

Or you can chain to the constructor you have in A from B's constructor. Ex:

public B (int id, string name) : base (id)
{
this.Name = name;
}
--
Tom Porterfield
Nov 26 '07 #2
Jeff wrote:
hey

.net 2.0

Below I declare 2 classes

******** class A ***********
public class A{
protected int _id;
public int Id
{
get { return _id; }
set { _id = value }
}

public A (int id)
{
this.Id = id;
}
}

******** class B ***********
public class B : A
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value }
}

public B (int id, string name)
{
this.Id = id;
this.Name = name;
}
}

In my .net code I've defined 2 similar classes. When I build the code I get
an compile error on the constructor of class B, it gives this error messsage
"No overload for method A takes 0 arguments"..
what am I missing here??
Hi,

I'm not a C# developer but I think I know whats going on.

The base class's (A) constructor is always called before the derived
classes constructor. In your case it is trying to call it with no
arguments, changing the constructor signature in B to.

public B (int id, string name):base(id)

Will make it pass the id parameter to your constructor in A.

The alternative would be to get rid of the constructor in A, but thats
up to you.

HTH,
Chris

Nov 26 '07 #3

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

Similar topics

2
by: Stephan Diehl | last post by:
I have a question about metaclasses: How would be the best way to "merge" different metaclasses? Or, more precisely, what is the best way to merge metaclass functionality? The idea is basicly...
17
by: Andrew Koenig | last post by:
Suppose I want to define a class hierarchy that represents expressions, for use in a compiler or something similar. We might imagine various kinds of expressions, classified by their top-level...
3
by: arserlom | last post by:
Hello I have a question about inheritance in Python. I'd like to do something like this: class cl1: def __init__(self): self.a = 1 class cl2(cl1): def __init__(self): self.b = 2
2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
1
by: Tony Johansson | last post by:
Hello Experts! I have some questions about inheritance that I want to have an answer to. It says "Abstract superclasses define a behavioral pattern without specifying the implementation" I...
3
by: Quentin Huo | last post by:
Hi: C# doesn't support multiple inheritance but it supports interface.But I think these ways are different. For example, C++ supports multiple inheritance. I have two classess classA and...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
8
by: Tony Johansson | last post by:
Hello! I wonder can somebody explain when is it suitable to use these methods OnKeyUp, OnKeyDown and OnKeyPress because these raise an event. These are located in class UserControl. If these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...
0
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...

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.