c# - Having trouble converting string to int -
in program have treeview. in section working with, node's displaynames numerical integer values, displayed strings. have come point in program need convert , temporarily store these displaynames in integer variable. use regex.match() no problem, in scenario getting compiler error: cannot implicitly convert type 'string' 'int'.
this code:
//this parent node may able see below //the children of node have displaynames integers var node = data.getallchildren(x => x.children).distinct().tolist().first(x => x.identify == 'b'); //get # of children -- if children exist if (node.children.count() > 0) { (int = 0; < node.children.count(); i++) { //error on line!!** intvalue = regex.match(node.children.elementat(i).displayname.value, @"\d+").value; } } *note: displayname.value string
to string int, use int.parse(string), returns int represented passed string , throws if input format incorrect.
int.parse(node.children.elementat(i).displayname.value) you can use int.tryparse if don't want throw. in case use:
int parsedvalue; if (int.tryparse(node.children.elementat(i).displayname.value, out parsedvalue)) { ///do whatever int }
Comments
Post a Comment