473,725 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can we override constructor

ad
When a class (say class1) inherited form another class (Class0).

The constructor in the class0 will be executing before the constructor of
class1.

Can we over the constructor?
Nov 16 '05 #1
7 37870
Depends on what kind of constructor it is - if it is the default constructor
(i.e. constructor with no parameters) then you can't override the parent's
constructor, it will always execute. If it is a non-default constructor then
it is automatically overriden unless you call it using the "base" keyword.
Note that the default constructor of the parent is always called regardless
of what kind of constructor is being invoked of the child class. Also,
whenever a base-class constructor executes, it does so before the inherited
class constructor is run.
Hope that helps,
~amit

"ad" <ad@wfes.tcc.ed u.tw> wrote in message
news:uJ******** ******@TK2MSFTN GP09.phx.gbl...
When a class (say class1) inherited form another class (Class0).

The constructor in the class0 will be executing before the constructor of
class1.

Can we over the constructor?

Nov 16 '05 #2
Amit wrote:
Note that the default constructor of the parent is always called regardless
of what kind of constructor is being invoked of the child class.


No, this is not true. You can specify which base constructor will be
called -

public ThisType(t1 p1, t2 p2): base(p1) { /*...*/ }

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 16 '05 #3
oops you're right... the default constructor of the parent class is always
called IF no other constructor is specified.
"Jon Shemitz" <jo*@midnightbe ach.com> wrote in message
news:41******** *******@midnigh tbeach.com...
Amit wrote:
Note that the default constructor of the parent is always called regardless of what kind of constructor is being invoked of the child class.


No, this is not true. You can specify which base constructor will be
called -

public ThisType(t1 p1, t2 p2): base(p1) { /*...*/ }

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 16 '05 #4
ad <ad@wfes.tcc.ed u.tw> wrote:
When a class (say class1) inherited form another class (Class0).

The constructor in the class0 will be executing before the constructor of
class1.

Can we over the constructor?


You can't override constructors, because they aren't inherited. It's
not entirely clear what you're after, but the following page may help:

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

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Amit wrote:
oops you're right... the default constructor of the parent class is always
called IF no other constructor is specified.


Yes, this is better. A base constructor will always be called, for
every class that descends from object, because every class must have
at least one constructor that calls a base() constructor (explicitly
or implicitly) and every call to a this() constructor must ultimately
call a base() constructor.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 16 '05 #6
"Jon Shemitz" <jo*@midnightbe ach.com> wrote in message
news:41******** *******@midnigh tbeach.com...
A base constructor will always be called, for
every class that descends from object, because every class must have
at least one constructor that calls a base() constructor (explicitly
or implicitly)


And because, one way or another, the base object must be constructed.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com


Nov 16 '05 #7
James Curran wrote:

"Jon Shemitz" <jo*@midnightbe ach.com> wrote in message
news:41******** *******@midnigh tbeach.com...
A base constructor will always be called, for
every class that descends from object, because every class must have
at least one constructor that calls a base() constructor (explicitly
or implicitly)


And because, one way or another, the base object must be constructed.


Yes. That's the semantic reason for the syntactic requirement.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 16 '05 #8

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

Similar topics

22
2955
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give me some hints on what might be wrong? // BEGIN OF SAMPLE CODE class Strategy
45
6351
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
2
3874
by: Josef Meile | last post by:
Hi, I have this constructor: public CExcelDatabase(string host, string user, string password, string database, bool promptCredentials, int findExcelInstance, bool readOnly) { //Some code comes here
4
2900
by: TJS | last post by:
How can I override the default values, of a class constructor , from an ASP.net page ? Public Sub New( ) HeaderStyle.BackColor = ColorTranslator.FromHtml("#5C85AD") HeaderStyle.ForeColor = Color.White HeaderStyle.Font.Bold = True End Sub
11
2411
by: RickHodder | last post by:
I'm having a problem, and here is a simplified example of code that demonstrates it: public class BizObj { public string TableName=""; private DataSet oData; public BizObj()
23
7234
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
2
3221
by: slolife | last post by:
See any problem with this code: Here's my Base page class: Public Class BasePage Inherits System.Web.UI.Page Protected m_NeedAlerts As Boolean Private Sub Page_Init(ByVal sender As Object, ByVal e As
34
3450
by: Smithers | last post by:
I have some logic that populates UI controls with values from App.config. A couple of checkboxes get checked or unchecked; and items loaded into a checked list box. Two reasonable places to put this code: 1. the form's constructor (or in a method called from within the constructor) 2. the Form_Load event procedure What are the important considerations or tradeoffs entailed by those two
0
8752
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3228
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.