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??
Tom Porterfield - 26 Nov 2007 11:24 GMT
> hey
>
[quoted text clipped - 38 lines]
> "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;
}

Signature
Tom Porterfield
Chris Diver - 26 Nov 2007 11:31 GMT
> hey
>
[quoted text clipped - 38 lines]
> "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