inheritence

J

juli jul

Hello,
I have a class (base class) a class which inherites it .
The inhereted class suppose to have all the features of the base class:
the public properties. I have an argument which the base class gets and
I sent this argument to dervied class constractor also.
I get this error:
No overload for method BaseClass() taking 0 arguments. why?
Thank you!
 
J

Jon Skeet [C# MVP]

juli said:
I have a class (base class) a class which inherites it .
The inhereted class suppose to have all the features of the base class:
the public properties. I have an argument which the base class gets and
I sent this argument to dervied class constractor also.
I get this error:
No overload for method BaseClass() taking 0 arguments. why?
Thank you!

See http://www.pobox.com/~skeet/csharp/constructors.html

In short, constructors aren't inherited.

If this doesn't help you, please post a short but complete example
which demonstrates the problem. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon
 
M

Michael S

In short, constructors aren't inherited.

Which we Delphites knows all too well! - What where you thinking Hejlsberg!?

Give it to us! Forget about LINQ; - We need virtual constructors like in
Delphi.. We're dying out here.... *trying to get up* ... *thump*

Well, I'm on a project that involves both .NET 2.0 and Delphi 7. Delphi
still rules! Polymorphic types is what makes the difference.

Anybody knows why C# took this path? Seriously. Why doesn't C# have
polymorphic types? While Delphi has primitive RTTI, .NET got reflection. I
can't see why you choose not to go polymporphic.

Tell me please...

Happy Coding
- Michael S

ps.
Mr. Skeet: I'm hoping for a good answer =)
 
P

patric.johansson

I don't get it, how can you inherit constructors? How will the derived
class be initialized if you skip its constructor and go directly for
the constructor of its base class?

Like in this example, which don't compile because of object dc2. But if
it did as apperently with Delphi, what would dc2.str and dc2.date be
set to? Which of DerivedClass's constructors would run? How does delphi
solve that?

public class BaseClass
{
int n;
public BaseClass (int n)
{
this.n = n;
}
}

public class DerivedClass : BaseClass
{
string str;
DateTime date;

public DerivedClass (string s) : base (5)
{
this.str = s;
this.date = DateTime.Today;
}

public DerivedClass (DateTime date) : base (5)
{
this.str = "hello";
this.date = date;
}

public static void Main()
{
DerivedClass dc1 = new DerivedClass("hi");
// dc1.n = 5, dc1.str = hi, dc1.date = todays date

DerivedClass dc2 = new DerivedClass(10);
// dc2.n = 5, dc2.str = ???, dc2.date = ???
}
}

Thanks, Patric
My C# blog: http://spaces.msn.com/members/pjsson
 
S

Scott Roberts

I don't get it, how can you inherit constructors? How will the derived
class be initialized if you skip its constructor and go directly for
the constructor of its base class?

Like in this example, which don't compile because of object dc2. But if
it did as apperently with Delphi, what would dc2.str and dc2.date be
set to? Which of DerivedClass's constructors would run? How does delphi
solve that?

Since the derived class didn't override the constructor with an int
parameter the dc2.str and dc2.date properties would remain uninitialized -
as you would expect.
 
D

django

Provide a parameter less constructor.

class AAA{
//parameter less ctor
public AAA(){

}
}
 
S

Scott Roberts

Michael S said:
Which we Delphites knows all too well! - What where you thinking
Hejlsberg!?

Give it to us! Forget about LINQ; - We need virtual constructors like in
Delphi.. We're dying out here.... *trying to get up* ... *thump*

Well, I'm on a project that involves both .NET 2.0 and Delphi 7. Delphi
still rules! Polymorphic types is what makes the difference.

Anybody knows why C# took this path? Seriously. Why doesn't C# have
polymorphic types? While Delphi has primitive RTTI, .NET got reflection. I
can't see why you choose not to go polymporphic.

Tell me please...

From Jon Skeet's page:

"Some people have said that they would rather constructors were inherited,
making the language act as if all derived classes had constructors with all
the parameter lists from the constructors from the base class, and just
invoking them with the parameters provided. I believe this would be a very
bad idea. Take, for instance, the FileInfo class. You must logically provide
a filename when constructing a FileInfo instance, as otherwise it won't know
what it's meant to be providing information on. However, as object has a
parameterless constructor, constructors being inherited would then mean that
FileInfo had a parameterless constructor. Some have suggested that this
could be fixed by allowing you to "override" the parameters you didn't want
invoked as private, but this goes against the idea that you should never be
able to override anything to give it more restrictive access, and also means
that class developers would have to change their code every time a new
constructor was added to a base class. "

http://www.yoda.arachsys.com/csharp/constructors.html

What exactly do you mean by polymorphic types?
 
D

django

Provide a parameter less constructor in the base class.

class AAA{
//parameter less ctor
public AAA(){

}
public AAA(int i){

}
}
class BBB: AAA{
static void Main(){
// call to parameter less ctor
AAA a = new AAA();
AAA aa = new AAA(10);

}
}
AAA a = new AAA() would give an error if the parameter less ctor is not
there in the base class.You must either define constructor as part of
your class definition or let the runtime provide one on your behalf.
 
B

Bjorn Abelli

...
I have a class (base class) a class which inherites it .
The inhereted class suppose to have all the features of the base class:
the public properties. I have an argument which the base class gets and
I sent this argument to dervied class constractor also.
I get this error:
No overload for method BaseClass() taking 0 arguments. why?

In short, if you created a constructor in the base class that needs an
argument, and at the same time *not* provided a constructor with 0
arguments, the constructor in derived classes need to specify which
constructor to use in the base class.

Example:

public class BaseClass
{
public BaseClass(string arg) { }
}

This means that the only way to create an instance of BaseClass, you need to
provide a string, *even* for derived classes.

Example of a derived class:

public class Derived : BaseClass
{
public Derived(string arg) : base(arg) { }
}


HTH,

// Bjorn A
 
J

Jon Skeet [C# MVP]

Michael S said:
Which we Delphites knows all too well! - What where you thinking Hejlsberg!?

Give it to us! Forget about LINQ; - We need virtual constructors like in
Delphi.. We're dying out here.... *trying to get up* ... *thump*

Well, I'm on a project that involves both .NET 2.0 and Delphi 7. Delphi
still rules! Polymorphic types is what makes the difference.

Anybody knows why C# took this path? Seriously. Why doesn't C# have
polymorphic types? While Delphi has primitive RTTI, .NET got reflection. I
can't see why you choose not to go polymporphic.

Well, I don't know about how Delphi handles things, but imagine if all
constructors were inherited in .NET. System.Object has a public
parameterless constructor, which would mean *all* types would have one.

What exactly would

new FileStream()

mean?
 
J

Jon Skeet [C# MVP]

Scott Roberts said:
Since the derived class didn't override the constructor with an int
parameter the dc2.str and dc2.date properties would remain uninitialized -
as you would expect.

You can't *override* a constructor with an int parameter - overriding
implies inheritance, and as I said before, constructors aren't
inherited.

Instead, the sample will fail on the call
new DerivedClass(10);
as there are *no* DerivedClass constructors which take an int as the
parameter.
 
S

Scott Roberts

You can't *override* a constructor with an int parameter - overriding
implies inheritance, and as I said before, constructors aren't
inherited.

He asked how it would work in Delphi - where you *can* override a
constructor.
Instead, the sample will fail on the call
new DerivedClass(10);
as there are *no* DerivedClass constructors which take an int as the
parameter.

That's why it *won't* work in C#, the question was how *would* it work in
Delphi.
 
S

Scott Roberts

What exactly do you mean by polymorphic types?

Nevermind. I think you mean polymorphic object creation using class
references. The same can be done in C# with the Activator class. Perhaps not
as elegant, but functionally equivalent.
 
J

Jon Skeet [C# MVP]

Scott Roberts said:
He asked how it would work in Delphi - where you *can* override a
constructor.

Ah, right. Missed that, sorry - a nasty day of fitting unit tests onto
unmanaged code has left me somewhat frazzled :(
 

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