0

关于使用类参数链接构造函数的问题。

我有一个带有列表框的表单。此表单用于调试目的。当我实例化我的所有对象(类)时,我希望他们在这个列表框中写下发生了什么。所以我将调试类作为参数传递给其他类,以便每个人(类)现在都是这个列表框。我通过委托回调传递文本以从每个类写入列表框调试。问题是其他人想调用我的类(而不是调试器)并且他们想向我发送一个字符串。所以我试图使用链式构造函数,这样当我实例化我的类时,调试器类在参数中,当他们调用我的类时,它们使用字符串作为参数。

代码在那里:

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;
    }
}

调试器:

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

OtherClass 共享调试器列表框:

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.");
        }
    }
}

对此有何评论?或者我可以把问题作为答案吗?

非常感谢你的代码工作者!

并且带有可选参数应该是:

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.");
        }
    }
}

如果我们在调用中分配名称(在 form1 中),它会起作用:

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

为什么我们必须分配这些?一些帮助?

这是问题所在。来自https://msdn.microsoft.com/en-us//library/dd264739.aspx:“如果调用者为连续可选参数中的任何一个提供参数,则它必须为所有前面的可选参数提供参数。”因此,如果我省略第一个可选选项,它将不起作用。我们必须把名字:所以它被迫得到好的。

4

3 回答 3

0

您需要修改这些构造函数

    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;
            }
        }
    }
于 2016-11-17T21:08:49.803 回答
0

您应该使用可选参数。就像:

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;
        }
    }
于 2016-11-17T20:59:49.243 回答
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.

于 2016-11-19T12:12:20.130 回答