Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DerivedClass Constructor calling base class constructor

Posted on 2013-01-31
8
Medium Priority
?
392 Views
Last Modified: 2013-02-01
For the instructions below- why is the :base() stated here when the base constructor with no parameters will be called anyways?

public class Derived: MyBaseClass
{
     public Derived() : base()
     {

     }
}
I see that on occasion using the :base(). Isn't the default cause it to call the base constructor anyway? So what is the purpose?
0
Comment
Question by:metro156
  • 4
  • 4
8 Comments
 
LVL 40
ID: 38842837
The base constructor is called by default only if you do not provide a constructor in your own class. From the moment that you define your own constructor, you take control, and you need to decide if you want to call the base constructor or not.
0
 

Author Comment

by:metro156
ID: 38843303
I ran this code below and it still called MyBaseClass default constructor. You see I am providing my own constructor.

public class Derived: MyBaseClass
{
     public Derived()
     {

     }
}
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 38844351
Oooops. After a short test I see that I am wrong, I know why, and this answer the question.

Some time in the past, I cannot say if it was in the first versions of the framework or when I was programming in C++ at the beginning of the 90's, you had to implicitely call the default constructor. So it almost became an automatism for older programmers. A lot of automatisms stays with us even when they are no longer needed.

Today's compilers probably add the call if you do not do it.

You see the same thing with the habit of setting local variables to null (or Nothing in VB) just before you leave a method. The need for that disappeared at the time of VB4, somewhere around 1995, but a lot of programmer still do it by habit, or because they learned from older programmers that have that habit and do not know that things have changed. Unfortunately, these programmers are also usually the ones that do not know that calling Dispose might do part of the job.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:metro156
ID: 38844817
Thank you. I thought that might be the case where older programmers leave code that is no longer needed in. And that's fine. No harm. By the way I am one of those types myself. LOL
Thanks again.
0
 
LVL 40
ID: 38844850
They should have special forums for old programmers :-)

But I would be surprised if you were such an old type as I am. There are very few of my generation who still code and are enjoying the ease with which we do complex things nowadays.

I started programming at a time where a keypunch was the easiest way to enter code in the system,  and you were lucky if you could compile a 50 lines Fortran program more than twice a day in the college computer lab. In some systems, entering code was still done by flippling binary switches.
0
 

Author Comment

by:metro156
ID: 38844885
Oh no, I did keypunch ibm cards. And I also did Fortran. I am trying to make a come back. I worked for AT&T for nearly 30 years and was outsourced to IBM. Subsequently, IBM outsourced my job after one year to IBM India. From there I took care of a very ill parent for nearly 3 years until they passed. Now I am trying to go back to work at 56 years old. I don't know going back to IT is justifiable but I am a techy at heart. I am studying the .NET world ( App Dev, WCF, ADO and ASP.NET). I hope to go back to work this year even it's at an entry level.
0
 
LVL 40
ID: 38845101
I am still a couple of years older than you are :-) and I earn my living very well by giving training in VB.NET all around the province of Quebec. Programmers in our age bracket can still be in the game if they enjoy it.

I do not know about the situation where you live, but here, the schools are not churning out IT professionals fast enough. There is a great demand. It's easy to find a job in the field.

And this also holds true for older technologies.

I had a student a few years back who was feeling very bad. RPG and COBOL on mainframe all his life. Absolutely unable to make the switch to the "new world" as he called it. He had a very hard time to grasp simple concepts such as events. He kept asking "I understand the concept, but where is the line that calls the Click?". For him, it was an alien world.

He was 63, close to retirement and feared that he would lose everything because he could not adapt to the new ways of programming. He cried a couple of times during the 4 days session.

I met him a few months ago. All smile. He is now past retirement age... and still working.

Shortly after getting out of my course with suicidal thoughts (I had over 4300 students, and he is the only one that came out that way, I assure you), he met a head hunter that changed his life. She told him how people with his knowledge were in demand. He ended up quitting his job by himself and started his own small consulting service.

"My hourly rate is 3 times your's" he told me. "I refuse more than half of the requests I get because I cannot keep up with the demand".

I wish you to find work in the .NET world. As far as I am concerned, it is a lot more interesting than the oldest ways of programmings. But if not, do not despair. It seems that in a few places, the old experience is still needed.
0
 

Author Comment

by:metro156
ID: 38845166
Interesting. Thanks the vote of confidence.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question