On Fri, 20 Jul 2007 12:05:22 -0700, <br************@gmail.comwrote:
Well how can I resolve this
Um. Either stop adding stuff to the list you're using as the termination
condition for your loop, or stop using as the termination condition for
your loop the count of the list you keep adding stuff to.
You might get a better answer if you could explain why it is you feel you
need to add new members to your list each iteration through the loop.
It's hard to say given the code you posted, since neither code example
seems all that useful. Maybe you meant to write something like this
instead:
for (iYear = 0; iYear < oYearsList.Count; iYear++)
{
sHtml.Append("<option value='" + oYearsList[iYear].ToString()
+ "'>" + oYearsList[iYear].ToString() + "</option>");
}
Which simply takes each object in the list, converts it to a string, and
includes the value in your output.
There are other aspects of the code that are troubling, such as the fact
that you're appending what looks like XML to something with "Html" in the
name, the fact that you appear to have redundant information in your
output, and the fact that there's no other formatting to the output (like
indentation and line breaks) that would make the output more readable by
humans (obviously not strictly necessary, but it sacrifices one
significant benefit of using text for data output).
But maybe those are all non-issues. You haven't described what you're
actually doing with enough detail for me or anyone else here to say for
sure those are problems.
Finally, just for completeness, here is a version of your code that
changes the behavior the least, while still fixing the infinite loop:
int cyears = oYearsList.Count;
for (iYear = 0; iYear <= cyears - 1; iYear++)
{
sHtml.Append("<option value='" + oYearsList.Add(iYear)
+ "'>" + oYearsList.Add(iYear) + "</option>");
}
All that does is get the count once, rather than retrieving it each time
through the loop. The rest of the code still looks wrong to me, but
hey...if that's what you really really wanted to do, the above is how to
get it to work as you presumably intended it to.
Pete