Search code examples
c#parameter-passingchainingmultiple-constructors

Constructor chaining with class as parameter


Question about chaining constructor with Class Parameter.

I have a Form with a list box. This form is for debug purpose. When I instanciate all my object (Class) I want them to write in this list box what is happened. So I pass the debug class as parameter to other class so that everybody (class) now who is this listbox. I pass the text with delegate callback to write from each class to the listbox debug. The problem is that others want to call my classes (not the debugger) and they want to send me a string. So I'm trying to use chained constructor so that when I instanciate my classes is with the debugger class in parameter and when THEY call my classes they do with a string as parameter.

Code is there:

public delegate void DEL_SetStringValCbk(string value);

public partial class Form1 : Form
{
    public  Debugger DebugConsole;
    internal OtherClass AnotherClass;
    internal OtherClass AnotherClassForString;
    public DEL_SetStringValCbk SetStringValCbk;
    private string MyTextToPass;
    public Form1()
    {
        InitializeComponent();
        MyTextToPass = "Hello world";

        DebugConsole = new Debugger();
        DebugConsole.Show();
        SetStringValCbk = new DEL_SetStringValCbk(DebugConsole.writeStr);
        SetStringValCbk("Object debugger done");
        AnotherClass = new OtherClass(DebugConsole);
        AnotherClassForString = new OtherClass(MyTextToPass);
        textBox1.Text = AnotherClassForString.TextReceived;
        textBox2.Text = AnotherClass.TextReceived;
    }
}

Debugger:

public partial class Debugger : Form
{
    public Debugger()
    {
        InitializeComponent();
    }
    public void writeStr(string valuestr)
    {
        lb_debuglist.Items.Add(valuestr);
    }
}

OtherClass sharing debugger listbox:

class OtherClass
{
    public string TextReceived = "none";
    public DEL_SetStringValCbk writeToDebug;
    Debugger DebuggerConsole;

    public OtherClass()//default ctor
    {}
    public OtherClass(string valuereceived): this(valuereceived, null)//only int ctor
    {}
    public OtherClass(Debugger _Debugger) : this("null", _Debugger) { }
    public OtherClass(string valuereceived, Debugger _Debugger)//master ctor
        : this()
    {
        TextReceived = valuereceived;
        if (_Debugger != null)
        {
            DebuggerConsole = _Debugger;
            writeToDebug = new DEL_SetStringValCbk(DebuggerConsole.writeStr);
            writeToDebug("class constructed init OK.");
        }
    }
}

Any remarks on this? Or can I put the question as answered?

Many thanks to you the codeworker!

And with optionnal parameter should be:

class OtherClass
{
    public string TextReceived = "none";
    public DEL_SetStringValCbk writeToDebug;
    Debugger DebuggerConsole;

    public OtherClass()//default ctor
    {}
    public OtherClass(Debugger _Debugger = null,string valuereceived = "")//master ctor with default param
        : this()
    {
        TextReceived = valuereceived;
        if (_Debugger != null)
        {
            DebuggerConsole = _Debugger;
            writeToDebug = new DEL_SetStringValCbk(DebuggerConsole.writeStr);
            writeToDebug("class constructed init OK.");
        }
    }
}

It works if we assign the name in the call like (in form1):

        AnotherClass = new OtherClass(_Debugger:DebugConsole);
        AnotherClassForString = new OtherClass(valuereceived:MyTextToPass);

Why do we have to assign these? some help?

Here is the problem. From https://msdn.microsoft.com/en-us//library/dd264739.aspx:" If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. " so if I omit the first optional it would not work. We have to put the name: so it is forced to get the good one.


Solution

  • You need to modify these constructors

        public class ClassToPass
        {
            public int num = 0;
        }
    
        class OtherClass
        {
            int Numeral = 2;
            ClassToPass classtestinside;
    
            public OtherClass()//default ctor
            {}
    
            public OtherClass(int valuereceived): this(valuereceived, null)//only int ctor
            {}
    
            public OtherClass( ClassToPass _Classtest)//classtopass ctor
            : this(0, _Classtest)
            {
            }
            public OtherClass(int valuereceived, ClassToPass _Classtest)//master ctor
                : this()
            {
                Numeral = valuereceived;
                if (_Classtest != null)
                {
                    classtestinside = _Classtest;
                    classtestinside.num = 34;
                }
            }
        }