0

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.

4
  • 3
    OtherClass does not have any constructor that expects only ClassToPass as a parameter. What do you expect? Commented Nov 17, 2016 at 17:08
  • It does not even have any constructor excepting a Form1 How should Form1 bind to a ClassToPass? I have the feeling you´re trying to make some kind of God-object that can handle all kinds of different types. This is a really bad idea and only shows that you´re doing too much in one single class. Commented Nov 17, 2016 at 17:15
  • Your constructor OtherClass(int valuereceived): this(valuereceived, ClassToPass missingsomething) isn't valid - you can't define a parameter like that. I don't know what you're trying to do. Commented Nov 17, 2016 at 17:24
  • 1
    Maybe C# optional parameters can help you. Commented Nov 17, 2016 at 17:34

3 Answers 3

0

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;
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You should use optional parameters. It would be like:

class OtherClass
 {
    int Numeral = 2;
    ClassToPass classtestinside;

    public OtherClass()//default ctor
    {}

    public OtherClass(int valuereceived = 0, ClassToPass _classtest = null)//master ctor
     : this()
    {
        Numeral = valuereceived;
        if(_classtest !=null)
        {               
            classtestinside = _classtest;
            classtestinside.num = 34;
        }
    }

2 Comments

Hello, It doesn't work for me Framework 4.0? But thanks
Hi, this should work, since I also use .NET4.0. What is the error?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.