?
Solved

c# constructor chaining

Posted on 2012-04-06
3
Medium Priority
?
373 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 668 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 664 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 668 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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