c# - Constructor chaining with class as parameter -
question chaining constructor class parameter.
i have form list box. form debug purpose. when instanciate object (class) want them write in list box happened. pass debug class parameter other class (class) listbox. pass text delegate callback write each class listbox debug. problem others want call classes (not debugger) , want send me string. i'm trying use chained constructor when instanciate classes debugger class in parameter , when call classes string parameter.
code 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 put question answered?
many codeworker!
and 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 default param : this() { textreceived = valuereceived; if (_debugger != null) { debuggerconsole = _debugger; writetodebug = new del_setstringvalcbk(debuggerconsole.writestr); writetodebug("class constructed init ok."); } } }
it works if assign name in call (in form1):
anotherclass = new otherclass(_debugger:debugconsole); anotherclassforstring = new otherclass(valuereceived:mytexttopass);
why have assign these? help?
here problem. https://msdn.microsoft.com/en-us//library/dd264739.aspx:" if caller provides argument 1 of succession of optional parameters, must provide arguments preceding optional parameters. " if omit first optional not work. have put name: forced one.
you need 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; } } }
Comments
Post a Comment