Solved

c# constructor chaining

Posted on 2012-04-06
3
340 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 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

21 Experts available now in Live!

Get 1:1 Help Now