Solved

DerivedClass Constructor calling base class constructor

Posted on 2013-01-31
8
369 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 500 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now