c# - How to access a ref bool variable passed into the constructor? -
i have main form, calls smaller form. in main form have bool called _dataready set false, purpose of smaller form check few things , see if data ready, in case sets _dataready true.
here problem: call mini form input parameter such (ref bool _dataready) problem don't have access outside of constructor block.
i tried making private bool , set ref that, after changing state of private bool ref did not take changes, unlike how object oriented programming works.
here of code:
//this how call mini form within main
new frmaccounting(mytextbox1.text.trim().replace(",", "").toint32(),ref _dataready).showdialog(); this constructor of mini form , code:
public frmaccounting(decimal price,ref bool _dataready) { initializecomponent(); dataready=_dataready; } private bool dataready; however setting private bool within form (dataready) true not change ref (again unlike how objects work)... that's thought happen.
my question is: how change ref can have results directly in main?
you can't using ref since field in class can't made ref.
i suggest wrap variable class. can pass class around , use inner value.
public class wrapper<t> { public t value {get;set;} } then use this:
wrapper<bool> w = new wrapper<bool>() { value = _dataready }; new frmaccounting(mytextbox1.text.trim().replace(",", "").toint32(),w).showdialog(); the value can retrieved afterwards using w.value.
Comments
Post a Comment