473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Novice question about inherited constructors

I am moving from Delphi to C# and hve encountered the problem:

I have the following classes and form Load event handler:

public class class1
{
public string S;
public class1( string aS )
{
S = aS;
}
}

public class class2 : class1
{
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
class2 C2 = new class2( "string" );
label1.Text = C2.S;
}

When I try to compile I get the following errors:

No overload for method 'class1' takes '0' arguments
No overload for method 'class2' takes '1' arguments

What does it mean ?
Why inherited constructors are not used (as in Delphi) ?

Oleg Subachev
Apr 10 '06 #1
12 1761
That is just part of the design of .NET that constructors are not inherited.
If you have a non-default constructor in a base class, you must also define
that same constructor and then pass the value up to its base.

public class2(string aS) : base(aS){...}
"Oleg Subachev" <ol**@urvb.ru > wrote in message
news:e$******** ******@TK2MSFTN GP02.phx.gbl...
I am moving from Delphi to C# and hve encountered the problem:

I have the following classes and form Load event handler:

public class class1
{
public string S;
public class1( string aS )
{
S = aS;
}
}

public class class2 : class1
{
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
class2 C2 = new class2( "string" );
label1.Text = C2.S;
}

When I try to compile I get the following errors:

No overload for method 'class1' takes '0' arguments
No overload for method 'class2' takes '1' arguments

What does it mean ?
Why inherited constructors are not used (as in Delphi) ?

Oleg Subachev

Apr 10 '06 #2
Oleg Subachev wrote:
I am moving from Delphi to C# and hve encountered the problem:
<snip>
When I try to compile I get the following errors:

No overload for method 'class1' takes '0' arguments
This is because the compiler is creating a default constructor for
class2, which tries to call a parameterless constructor in class1 - but
there isn't one.
No overload for method 'class2' takes '1' arguments
This is because there isn't a constructor in class2 taking a string,
because constructors aren't inherited.
Why inherited constructors are not used (as in Delphi) ?


If constructors were inherited, then *all* classes would have to have a
parameterless constructor, as System.Object does. I don't know how
Delphi gets around it, but personally I'm glad constructors aren't
inherited - it's good to be able to mandate that any instance of a
certain class *must* go through one of the constructors I've specified,
even if extra constructors have been added to the base class.

I view instance creation as somewhat different to instance use - things
which can be treated the same way after they've been created often have
very different creation requirements.

(Having said that, I *would* like some way of specifying what
constructors *must* be present in any type implementing a specific
interface, and possibly likewise for static methods. Extra syntax would
be necessary in order to call those constructors/static methods,
however - it does all get a bit complicated.)

Jon

Apr 10 '06 #3
Hey Jon,
(Having said that, I *would* like some way of specifying what
constructor s *must* be present in any type implementing a specific
interface, and possibly likewise for static methods. Extra syntax would
be necessary in order to call those constructors/static methods,
however - it does all get a bit complicated.)


I think that Idea causes some problems with the concept of an
interface. But I would like to a concept similar to an interface called
a "performa", the performa being what you described above. I feel some
tasks just don't relate to an interface and need something similar to
friend classes or something.

A classic example would be ISerializable. The concept of the interface
is not enought to encapuslate deserialisation (which requires a special
constructor).

It would be nice if the constructor often found when using
ISerializable was enforced. As such the serilaisation/deserialsation
requires a concept stronger than an interface.

So basicly Im with you, we need a way to enforce constructors... .

I do however recall an article "somewhere" that explains why this is
"might" not possible under the CLR.

-dm

Apr 10 '06 #4
<th*********@gm ail.com> a écrit dans le message de news:
11************* *********@i40g2 00...legr oups.com...

| It would be nice if the constructor often found when using
| ISerializable was enforced. As such the serilaisation/deserialsation
| requires a concept stronger than an interface.

How about a constraint like with generics ?

public interface ISerializable requires new()
{

}

....or perhaps a special method syntax, like the property declaration :

public interface ISerializable
{
.ctor();
}

What think you ???

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Apr 10 '06 #5
Joanna Carter [TeamB] wrote:
| It would be nice if the constructor often found when using
| ISerializable was enforced. As such the serilaisation/deserialsation
| requires a concept stronger than an interface.

How about a constraint like with generics ?


That would be fine, except the only constraint available is that
there's a public parameterless constructor. It would often be nice to
be able to specify constructors with parameters, and this ability
doesn't exist with generics.

In addition, serialization itself recommends using a protected or
private constructor, which quite reasonably can't be expressed as a
constraint. Serialization is a pretty specialised area - I wouldn't
want to bend the platform too much to aid it. However, various things
which are dynamically loaded would benefit from enforcing some way of
constructing an instance (whether by a factory method or a
constructor).

Jon

Apr 10 '06 #6
How is
public class A
{
public A(){}
}
different from
public class B : A
{
public B() : base()
{}
}
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
Oleg Subachev wrote:
I am moving from Delphi to C# and hve encountered the problem:


<snip>
When I try to compile I get the following errors:

No overload for method 'class1' takes '0' arguments


This is because the compiler is creating a default constructor for
class2, which tries to call a parameterless constructor in class1 - but
there isn't one.
No overload for method 'class2' takes '1' arguments


This is because there isn't a constructor in class2 taking a string,
because constructors aren't inherited.
Why inherited constructors are not used (as in Delphi) ?


If constructors were inherited, then *all* classes would have to have a
parameterless constructor, as System.Object does. I don't know how
Delphi gets around it, but personally I'm glad constructors aren't
inherited - it's good to be able to mandate that any instance of a
certain class *must* go through one of the constructors I've specified,
even if extra constructors have been added to the base class.

I view instance creation as somewhat different to instance use - things
which can be treated the same way after they've been created often have
very different creation requirements.

(Having said that, I *would* like some way of specifying what
constructors *must* be present in any type implementing a specific
interface, and possibly likewise for static methods. Extra syntax would
be necessary in order to call those constructors/static methods,
however - it does all get a bit complicated.)

Jon

Apr 10 '06 #7
David <Co*********@Ad sorptionProcess Modeling.com> wrote:
How is
public class A
{
public A(){}
}
different from
public class B : A
{
public B() : base()
{}
}


The first declares a type which derives from System.Object
(implicitly). The second declares a type which derives from A. Both
declare a public parameterless constructor which calls (implicitly in
the first case) the parameterless constructor in the base class.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '06 #8
Thank you. I am actually trying to figure out why a TreeView works when
nodes are constructed using the TreeNode class but do not work when using a
class derived from TreeNode.

What is the difference between

public class TreeNode
{
public TreeNode()
{
}

}

and

public class B : TreeNode
{
public B : base()
{}

}

?

Why does an index go out of range in LoadViewState with the later.
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
David <Co*********@Ad sorptionProcess Modeling.com> wrote:
How is
public class A
{
public A(){}
}
different from
public class B : A
{
public B() : base()
{}
}


The first declares a type which derives from System.Object
(implicitly). The second declares a type which derives from A. Both
declare a public parameterless constructor which calls (implicitly in
the first case) the parameterless constructor in the base class.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 10 '06 #9
David <Co*********@Ad sorptionProcess Modeling.com> wrote:
Thank you. I am actually trying to figure out why a TreeView works when
nodes are constructed using the TreeNode class but do not work when using a
class derived from TreeNode.

What is the difference between

public class TreeNode
{
public TreeNode()
{
}

}

and

public class B : TreeNode
{
public B : base()
{}

}

?
Not a lot.
Why does an index go out of range in LoadViewState with the later.


I'm afraid I don't know.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '06 #10

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

Similar topics

3
1761
by: SLE | last post by:
Hi there, I know constructors are not inherited from the base class, not in VB.NET nor C# (nor Java I suppose). I never wondered,but reflecting on the reason why, I cannot find a solid answer. Is the reason technical (compiler or CLR limitation) or logical (OOP best practices)? Any feedback would be greatly appreciated.
3
1425
by: Alexander Muylaert | last post by:
Hi Is their a way to keep constructors visible in inherited classes? public class X{ public x(int A){} } public class Y : X{};
2
1478
by: PIEBALD | last post by:
OK, here's my latest workaround for the lack of inherited constructors... It requires that the class have a parameterless (default) constructor and a public virtual "Initialize" method (which returns its "this"). An instance (in this case a Number) is constructed and initialized like so: Number n = ((Number)((Number)...
3
1242
by: M O J O | last post by:
Hi, I've created a MasterForm which all my forms in my project must derive from. In my MasterForm, I've overloaded the New event with this code: Public Sub New(ByVal SomeText As String) MyBase.New()
10
1212
by: Kevin Buchan | last post by:
I searched the news group and could not find an answer to this question, so I'll go ahead and post it. Let's say I have a class A with a couple different constructors... nothin' special. Now, let's say that I have class B that inherits from A. B automagically gets all of the properties and methods of A, but I cannot leverage any of A's...
3
5465
by: Wayne Brantley | last post by:
VS2005 RTM Create a web user control to use as a base class for other web user controls. Now, create a new web user control, change the class it inherits from to your base class and compile. (You must have a <% Register %> so it will see it) You will get TWO warnings per class like:
15
1886
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following two? (1)
12
1497
by: Mike - EMAIL IGNORED | last post by:
Within class MyClass, I can think of two ways to tell if MyClass is inherited in a particular use: 1. pass an appropriate bool in the ctor args; 2. use a virtual method that returns, for example, a siring containing the class name. Is there a better way?
2
1558
by: Søren M. Olesen | last post by:
Hi How do I create an instance of an object with an inherited cunstructor?? In the below example, I'm able to create an instance of MyClass2 using the forst two lines of code, however if I try using the next two lines of code it fails.
0
7612
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...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7673
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
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...
1
5513
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...
0
5219
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...
0
3653
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.