-1

I have created a class that looks similar to the one below. As you can see I created a few constructors that I am trying to chain using : this()

class RTTutils
{
    #region Variables

    private bool verbose = false;
    private bool canWrite = false;

    private int x;
    private int y;

    public RTTutils()
    {
        x = 5;

        y = 5;

        RTTCalc();
    }

    public RTTutils(int samples, bool verbose) : this()
    {
        this.verbose = verbose;
        this.samples = samples;
    }

    public RTTutils(int samples, bool verbose, bool canWrite) : this()
    {
        this.verbose = verbose;
        this.samples = samples;
        this.canWrite = canWrite;
    }

    public RTTutils(int samples) : this(samples, false, false)
    {
    }

    public RTTutils(bool verbose) : this()
    {
        this.verbose = verbose;
    }

    private void RTTCalc()
    {
        if (this.verbose)
            Console.WriteLine("Test");
    }

I am trying to initialize it using

RTTutils rttcalculator = new RTTutils(true);

or any other combination for verbose and canWrite, they still remain false though. As an example in this case we will see nothing printed in the console, even though I indicated true when initializing the class.

What am I doing wrong in this case?

dearn44
  • 3,198
  • 4
  • 30
  • 63
  • 2
    You need to provide an example that can actually reproduce your problem. As it is it is not complete enough to demonstrate your problem. – Servy Nov 30 '16 at 17:29
  • "they still remain `false` though", when, where, and how are you obtaining this information? – Quantic Nov 30 '16 at 17:30
  • Related: http://stackoverflow.com/q/1814953/215552 – Heretic Monkey Nov 30 '16 at 17:33
  • You expect (wrongly) boolean class fields used in method `RTTCalc` to have values you set in constructors with parameters. However, the parameterless constructor executes before these assignments. – Igor Nov 30 '16 at 17:40
  • @Igor shouldn't the constructor `public RTTutils(bool verbose) : this()` be executed first, since that's what Im calling. – dearn44 Nov 30 '16 at 17:42
  • In `public RTTutils(bool verbose) : this()` the parameterless constructor is run before the one with the `verbose` parameter. That means `RTTCalc` is run before the `verbose` field is set to `true` in the chaining constructor. – Lee Nov 30 '16 at 17:49

2 Answers2

0

You expect (wrongly) boolean class fields used in method RTTCalc to have values you set in constructors with parameters. However, the parameterless constructor executes before these assignments.

Do not call RTTCalc in parameterless constructor. Provide static factory methods instead:

class RTTutils
{
    private bool verbose = false;
    private bool canWrite = false;

    private RTTutils()
    {
        sampleList.Add(100);    // First sample should be 100
        optionChosen.Add("E");

        x = 5;
        y = 5;

        System.IO.File.Delete(this.path);
    }

    private RTTutils(bool verbose) : this()
    {
        this.verbose = verbose;
    }

    private void RTTCalc()
    {
        if (this.verbose)
            Console.WriteLine("Test");
    }    

    public static RTTutils Create(bool verbose)
    {
      RTTutils result = new RTTutils(verbose);
      result.RTTCalc();
      return result;
    }
}
Igor
  • 15,833
  • 1
  • 27
  • 32
0

Given your code above, I rewrote it and it initializes verbose and canWrite as expected.

class Foo {

    private bool _verbose = false;
    private bool _canWrite = false;
    private int _samples;
    private int x;
    private int y;


    public Foo(int samples, bool verbose, bool canWrite)
    {
        _verbose = verbose;
        _canWrite = canWrite;
        _samples = samples;
        x = 5;
        y = 5;

        RTTCalc();
    }
    public Foo() : this(0, false, false) { }

    public Foo(int samples) : this(samples, false, false) { }

    public Foo(int samples, bool verbose) : this(samples, verbose, false) { }


    private void RTTCalc()
    {
        Console.WriteLine($"V={_verbose}, S={_canWrite}");
        Console.ReadKey();
    }

}

class Program
{
    static void Main(string[] args)
    {
        Foo test1 = new Foo(1, true, false);
        Foo test2 = new Foo(1, true);
        Foo test3 = new Foo();

    }
}

Does this work for you? If not then you are doing something else that is not shown in your code that is affecting verbose and canWrite.

Ibrahim Malluf
  • 657
  • 4
  • 6