C# 函数未按预期返回字符串
正确的。这可能是一个根本性的错误,但我仍然想念它......下面的第一个函数将字符串输出到控件 cbContent2。第二个返回任何空字符串 - 它需要返回与第一个相同的字符串。
函数 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;
}
函数 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);
}
我认为 cbContent2.Text += 相关和 return(相关) 之间的差异出了问题 - 关于如何使 Function2 产生与 Function1 相同的输出的任何想法都将是伟大的...
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 return 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试更改
为
您应该始终尽量避免在字符串循环中使用“+=”。由于它们是不可变的,因此您必须为循环中的每次迭代创建一个副本。对于小型集合,您可能不会注意到性能受到影响,但对于大型集合,这会对性能产生巨大影响。尝试使用 StringBuilder() 来构建字符串。 StringBuilder 效率更高。
Try changing
to
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.
你需要去相关 += getRelatedNews( ... )
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?
If so change it to this:
Also change this line
to
其实问题就在这里:
Actually the problem is here: