C# function not returning string as expected
Right. This is probably a funadmental mistake, but I'm still missing it... The first function below outputs a string to the control cbContent2. The second returns any empty string - it needs to r开发者_如何学编程eturn the same string as the first.
Function 1
private void getRelatedNews(TaxonomyData taxData, string related, string contentTitle)
{
foreach (TaxonomyItemData item in taxData.TaxonomyItems)
{
if (taxData.TaxonomyName.Equals(contentTitle) && taxData.TaxonomyItemCount != 0)
{
related += string.Format("<li><a href='{0}'\">{1}</a></li>", item.TaxonomyItemId.ToString(), item.TaxonomyItemId.ToString());
}
}
// Show all its sub categories
foreach (TaxonomyData cat in taxData.Taxonomy)
{
getRelatedNews(cat, related, contentTitle);
}
cbContent2.Text += related;
}
Function 2
private string getRelatedNews(TaxonomyData taxData, string related, string contentTitle)
{
foreach (TaxonomyItemData item in taxData.TaxonomyItems)
{
if (taxData.TaxonomyName.Equals(contentTitle) && taxData.TaxonomyItemCount != 0)
{
related += string.Format("<li><a href='{0}'\">{1}</a></li>", item.TaxonomyItemId.ToString(), item.TaxonomyItemId.ToString());
}
}
// Show all its sub categories
foreach (TaxonomyData cat in taxData.Taxonomy)
{
getRelatedNews(cat, related, contentTitle);
}
return(related);
}
I think something is going wrong in the difference between cbContent2.Text += related and return(related) - any ideas on how to make Function2 produce the same output as Function1 would be grand...
Try changing
getRelatedNews(cat, related, contentTitle);
to
related += getRelatedNews(cat, related, contentTitle);
You should always try to avoid "+=" in a loop with strings. Since they are immutable you have to make a copy for each iteration in the loop. For a small collection you might not notice a performance hit but for large collections its a huge impact on performance. Try to use StringBuilder() for building a string. StringBuilder is much more efficient.
you need to go related += getRelatedNews( ... )
Strings are immutable. You cannot change a string by using "+=".
You are setting your parameter related
to a new string, but you aren't changing the string that was passed in.
The problem is related to (wait for it) related
. You have recursion happening. In the first function, you are recursively calling into the function and you always concatenate the result to the control at the end of the function (reached in every recursive execution). In the second, you're still acting recursively, but your never capturing the result from those recursive calls. The string is immutable, related
is not updated by these recursive function calls automatically. Start capturing the result and evaluate it to see if the results are what you expect.
What does the call to the function look like? Are you doing something like this?
string blah = getRelatedNews(x, "", y);
If so change it to this:
string buffer,blah = getRelatedNews(x, buffer, y);
Also change this line
getRelatedNews(cat, related, contentTitle);
to
related += getRelatedNews(cat, related, contentTitle);
Actually the problem is here:
private string getRelatedNews(TaxonomyData taxData, string related, string contentTitle)
{
foreach (TaxonomyItemData item in taxData.TaxonomyItems)
{
if (taxData.TaxonomyName.Equals(contentTitle) && taxData.TaxonomyItemCount != 0)
{
related += string.Format("<li><a href='{0}'\">{1}</a></li>", item.TaxonomyItemId.ToString(), item.TaxonomyItemId.ToString());
}
}
// Show all its sub categories
foreach (TaxonomyData cat in taxData.Taxonomy)
{
// this code
getRelatedNews(cat, related, contentTitle);
// should be changed to
related += getRelatedNews(cat, related, contentTitle);
}
return(related);
}
// this code
getRelatedNews(cat, related, contentTitle);
// changed to
related = getRelatedNews(cat, related, contentTitle);
// works
精彩评论