Solved

c# constructor chaining

Posted on 2012-04-06
3
365 Views
Last Modified: 2012-04-06
Hello,

What does :this(0) mean ??   at line 10



using System;

namespace Teller03 {
  class Teller {
    public const int minValue = 0;
    public const int maxValue = 5;
    private int curValue; // default value 0

    public Teller()
      : this(0) { // explicit parameterless constructor
    }/*Teller*/

    public Teller(int initValue) {
      curValue = initValue;
    }/*Teller*/

    public int GetCurrentValue() { // accessor - getter
      return curValue;
    }/*GetCurrentValue*/

    public void SetCurrentValue(int newValue) { // mutator = setter
      if ((minValue <= newValue) && (newValue <= maxValue)) {
        curValue = newValue;
      }
    }/*SetCurrentValue*/

    public void Increment() {
      curValue++;
    }/*Increment*/

    public void Decrement() {
      curValue--;
    }/*Increment*/
  }/*Teller*/
}/*Teller03*/

Open in new window

0
Comment
Question by:kensy11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Accepted Solution

by:
Imran Javed Zia earned 167 total points
ID: 37816178
Hi,

When you will make an instance of Teller withn default constructor (constructor with no parameter) then it will first call parameter with int parameter and therefore you may get curValue equal to 0.

Teller t = new Teller();
MessageBox.Show(t.GetCurrentValue().tostring()); // will show you 0

Thanks
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 166 total points
ID: 37816179
It means the current constructor (line 9) is going to invoke the constructor which has a matching signature as what is being called in line 10. In this case, the constructor in line 13 will be invoked. Line 13's constructor will complete its initialization(s) (execution) before continuing with the logic within line 10's constructor.
0
 
LVL 7

Assisted Solution

by:Ironhoofs
Ironhoofs earned 167 total points
ID: 37816182
The Teller function without parameters calls Teller(int initValue) with value 0. Its a "nice to have", without it you would be forced to allways create Teller objects with a initial value (new Teller(0);)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

691 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