Solved

c# constructor chaining

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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