Why can't I call a parameterless-constructor from a baseclass ?

A

AndreasW

Hi,

internal static class Program
{

public class foo
{
private int _id;
public foo()
{
}
public foo(int _id)
{
this._id = _id;
}
}
public class bar : foo
{


}
[STAThread]
private static void Main()
{
bar b = new bar(3); // constructor canot be found
}
}

why is this code not allowed ?

thanks

AndreasW
 
B

Bruce Wood

AndreasW said:
Hi,

internal static class Program
{

public class foo
{
private int _id;
public foo()
{
}
public foo(int _id)
{
this._id = _id;
}
}
public class bar : foo
{


}
[STAThread]
private static void Main()
{
bar b = new bar(3); // constructor canot be found
}
}

why is this code not allowed ?

.... because in C# constructors are not inherited. If you want a
constructor for bar that takes an int, you have to say this:

public bar(int id) : base(id) { }

* I should add a note about "constructors are not inherited": if you do
not specify any constructor at all for a class, the compiler infers the
following:

public bar() : base() { }

If you then insert the int constructor, as above, you'll lose this
default constructor unless you explicitly write it. So, at the end of
it all, of you want bar to have constructors as foo, you need to say:

public bar() : base() { }
public bar(int id) : base(id) { }

By the way, one typically does not use the _ prefix for method
parameter names, just for private field names, so your foo constructor
would be better written as:

public foo(int id)
{
this._id = id;
}
 
T

Tom Porterfield

AndreasW said:
Hi,

internal static class Program
{

public class foo
{
private int _id;
public foo()
{
}
public foo(int _id)
{
this._id = _id;
}
}
public class bar : foo
{


}
[STAThread]
private static void Main()
{
bar b = new bar(3); // constructor canot be found
}
}

why is this code not allowed ?

Your bar class has no constructor that takes an argument of type int. When
you inherit from a base class, you do not inherit constructors, so you need
to provide your own constructor to bar that takes an int parameter. In that
constructor you can call the base constructor. Ex:

public class bar : foo
{
public bar()
{
}

public bar(int _id) : base(_id)
{
}
}
 
S

Sericinus hunter

Constructors are not inherited, you have to declare it in bar class:

public class bar : foo
{
public bar(int _id) : base(_id)
{
}
}
 
M

Martin Z

Are you positive about that implicit constructor inheritance? If so,
that could make my life much simpler - constructors are such a headache
for reuse and whatnot that I just tend to do lazy instantiation on
properties instead and avoid constructors altogether. You sure that's
not just the default constructor in action? Or is the default
constructor actually calling base()?

Bruce said:
AndreasW said:
Hi,

internal static class Program
{

public class foo
{
private int _id;
public foo()
{
}
public foo(int _id)
{
this._id = _id;
}
}
public class bar : foo
{


}
[STAThread]
private static void Main()
{
bar b = new bar(3); // constructor canot be found
}
}

why is this code not allowed ?

... because in C# constructors are not inherited. If you want a
constructor for bar that takes an int, you have to say this:

public bar(int id) : base(id) { }

* I should add a note about "constructors are not inherited": if you do
not specify any constructor at all for a class, the compiler infers the
following:

public bar() : base() { }

If you then insert the int constructor, as above, you'll lose this
default constructor unless you explicitly write it. So, at the end of
it all, of you want bar to have constructors as foo, you need to say:

public bar() : base() { }
public bar(int id) : base(id) { }

By the way, one typically does not use the _ prefix for method
parameter names, just for private field names, so your foo constructor
would be better written as:

public foo(int id)
{
this._id = id;
}
 
M

Martin Z

Just looked it up myself - yep, Base() is always implicitly called in
derived constructors unless otherwise indicated, and this would
logically extend to the default constructor. Should've realized, it's
obvious really.... dunno where I got the idea it could be any other
way.

Martin said:
Are you positive about that implicit constructor inheritance? If so,
that could make my life much simpler - constructors are such a headache
for reuse and whatnot that I just tend to do lazy instantiation on
properties instead and avoid constructors altogether. You sure that's
not just the default constructor in action? Or is the default
constructor actually calling base()?

Bruce said:
AndreasW said:
Hi,

internal static class Program
{

public class foo
{
private int _id;
public foo()
{
}
public foo(int _id)
{
this._id = _id;
}
}
public class bar : foo
{


}
[STAThread]
private static void Main()
{
bar b = new bar(3); // constructor canot be found
}
}

why is this code not allowed ?

... because in C# constructors are not inherited. If you want a
constructor for bar that takes an int, you have to say this:

public bar(int id) : base(id) { }

* I should add a note about "constructors are not inherited": if you do
not specify any constructor at all for a class, the compiler infers the
following:

public bar() : base() { }

If you then insert the int constructor, as above, you'll lose this
default constructor unless you explicitly write it. So, at the end of
it all, of you want bar to have constructors as foo, you need to say:

public bar() : base() { }
public bar(int id) : base(id) { }

By the way, one typically does not use the _ prefix for method
parameter names, just for private field names, so your foo constructor
would be better written as:

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

AndreasW

Hi,

Constructors are not inhertied. Ok. That's the reason.
Hm, may be, that I should figure out the advantages and disadvantages of implizit constructor inheritance.


thanks altogether.


AndreasW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top