c# - why does parse function return o? -
i new c# programming , bumped 1 problem looks pretty basic.i store string value sv_1 in variable lastserviceno , split using split function , result stored in string array called index.basically index[1] has numeric value bt string. want convert string int. in following code , behaves expected until parse function encountered.i not understand why parse function returning 0 index[1] has numeric value in it. can point problem please??
public string generateserviceno() { dataaccesslayer.dataaccesslayer dlobj= new dataaccesslayer.dataaccesslayer(); string lastserviceno = dlobj.getlastserviceno(); string[] index = lastserviceno.split('_'); int lastindex = int.parse(index[1]); return "sv_"+(lastindex++).tostring(); }
int.parse(string s) throws exception if number bug in terms of data size or string "s" not in correct numerical format.
the format method accepts "[ws][sign]number[ws]" where:
- [ws] optional 1 or more whitespace(" ")
- [sign] optional "+" or "-"
check here full reference.
thus said, can assure if int.parse(index[1]) returns 0 means index[1] equals "[ws][sign]0[ws]" using transcript above.
however, looking @ code, can conclude you're incrementing local variable after assignment without using incremented value afterwards. perhaps meant operation shouldn't 0?
if that's case believe you're trying achieve:
public string generateserviceno() { dataaccesslayer.dataaccesslayer dlobj= new dataaccesslayer.dataaccesslayer(); string lastserviceno = dlobj.getlastserviceno(); string[] index = lastserviceno.split('_'); int lastindex = int.parse(index[1]); return string.format("sv_{0}", ++lastindex); }
assuming index[1] == "0", method return "sv_1".
Comments
Post a Comment