Solved

c# constructor chaining

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
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 tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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

17 Experts available now in Live!

Get 1:1 Help Now