c# - Local variable unassigned issue using Try, catch, finally -
i wondering if hand please. can explain me why string sqrt
unassigned in finally
block? why have declare it? why can't declared in try or catch statement? make coding less tedious , more organized.
private void btndisplay_click(object sender, eventargs e) { int number; string sqrt; try { number = convert.toint32(tbinput.text); //why cant have "number=convert.todouble(tbinput.text)?// convert.todouble(number); if (number < 0) { throw new negativenumberexception(); } sqrt = math.sqrt(number).tostring(); } catch (formatexception error) { lboutput.items.add(error.message); lboutput.items.add("the input should number."); sqrt = "not able calculated"; } catch (negativenumberexception neg) { lboutput.items.add(neg.message); sqrt = "not able calculated"; } { //here having issue "unassigned local variable"// lboutput.items.add("square root " + sqrt); } } class negativenumberexception : exception { public negativenumberexception() : base("number can’t negative") { } } } }
what attempting achieve in block "square root" , "sqrt" displayed in list box no matter value of sqrt is. if output sqrt list box in of other blocks, works (because has been declared). know how can can this? bet it's simple too. don't mean rant or have been last 12 hours begin feel defeated. appreciate everyone's help, do.
change :
int number; string sqrt;
update :
double number = 0.0; string sqrt = string.empty;
Comments
Post a Comment