Identifying the first iteration of a loop, c#
pThis is very basic but i was wondering if there was a better way of
programming the following concept. /p precode for (int j = 0; j lt;
node.ChildNodes[i].Attributes.Count; j++) { if (j != 0) row.Cells[1].Value
+= , ; row.Cells[1].Value += node.ChildNodes[i].Attributes[j].Name; }
/code/pre pBasically im outputting nodes in c# to a table and i want each
attribute name to be separated with a comma. The issue is that obviously
for the first instance of the loop i wish not to have the comma preceded
it, ie i could not just have /p precoderow.Cells[1].Value += , +
node.ChildNodes[i].Attributes[j].Name; /code/pre potherwise the output in
the cell would look something like:/p precode, name, day /code/pre
pinstead of /p precodename, day /code/pre pSo although this works, it
seems a waste of the computer time to check each time it loops that this
is indeed the first iteration of the loop, especially as this loop is
nested in a recursive method. Is there a better way of doing this? /p
p(Bearing in mind that node.ChildNodes[i].Attributes.Count in the for loop
conditions may be 0, ie node(which is a xmlNode) could have no child
nodes, so the the loop doubles up as an existence check for the children
too. )/p pI hope i explained this well!/p
No comments:
Post a Comment