c# - How to handle values of values of values -
i'm reading in text file contains info 3d elements , store them in dictionary dict
in c#. main objects open_shell
s , closed_shell
s. these contain multiple advanced_face
s. these 1 time again contain single face_outer_bound
, multiple face_bound
s. these 1 time again contain more values , on until there numerical values.
for have class step
contains
list<list>string>> closedshell; //contains closed shells values list<list<string>> openshell; //contains open shells values list<list<string>> closedshelladvface; //contains closed advanced faces... list<list<string>> openshelladvface; //contains open advanced faces... ...
i iterate through each list next values , on. doesn't seem efficient i'm using duplicate code closed , open lists. examplary code this:
string closedshellkey = ""; string closedshellvalue = ""; string openshellkey = ""; string openshellvalue = ""; // closed_shells (int shelllistindex = 0; shelllistindex < stepobj.getclosedshells().count; shelllistindex++) { (int valuescount = 1; valuescount < stepobj.getclosedshells()[shelllistindex].count - 1; valuescount++) { if (dict.containskey(stepobj.getclosedshells()[shelllistindex][valuescount])) { closedshellkey = stepobj.getclosedshells()[shelllistindex][valuescount]; dict.trygetvalue(closedshellkey, out closedshellvalue); stepobj.setcsadvface(splitvalues(closedshellvalue)); } else { //throw exception } } } // open_shells (int shelllistindex = 0; shelllistindex < stepobj.getopenshells().count; shelllistindex++) { (int valuescount = 1; valuescount < stepobj.getopenshells()[shelllistindex].count - 1; valuescount++) { if (dict.containskey(stepobj.getopenshells()[shelllistindex][valuescount])) { openshellkey = stepobj.getopenshells()[shelllistindex][valuescount]; dict.trygetvalue(openshellkey, out openshellvalue); stepobj.setosadvface(splitvalues(openshellvalue)); } else { //throw exception } } }
this goes on next values, etc. , efficient way implement each of these steps? maybe create openshellobject
, closedshellobject
farther seperate? how handle info contains different info 1 time again contains farther different data, etc.?
hope clear enough
first, note dictionary.trygetvalue
work of dictionary.containskey
need former.
if understand this, need iterate on multiple collections, applying operation varies in 1 step, according each collection category, e.g. closed face, open face, etc. how separate step iteration code? i'd suggest either template method pattern or method parameter (map). problem i'd take map because collection items vary category not info type, , means less coding.
in pseudocode below i've assumed final step in each iteration involves method stepobj.setcsadvface
takes string[]
value returned splitvalues
. why, in apply method below, parameter method
delegate takes string[]
parameter, matches either stepobj.setcsadvface
or stepobj.setosadvface
, whichever required relevant collection.
private void apply(list<list<string>> list, action<string[]> method) { foreach (var items in list) { (int valuesindex = 1; valuesindex < items.count - 1; valuesindex++) { var key = items[valuesindex]; string values; if (dict.trygetvalue(key, out values)) { method(splitvalues(values)); } else { //throw exception } } } } apply(stepobj.getclosedshells(), stepobj.setcsadvface); apply(stepobj.getopenshells(), stepobj.setosadvface); ...
c# list object recursion
No comments:
Post a Comment