C# 函数未按预期返回字符串

发布于 2024-10-31 06:24:03 字数 1573 浏览 2 评论 0原文

正确的。这可能是一个根本性的错误,但我仍然想念它......下面的第一个函数将字符串输出到控件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

盗心人 2024-11-07 06:24:03

尝试更改

getRelatedNews(cat, related, contentTitle);

related += getRelatedNews(cat, related, contentTitle);

您应该始终尽量避免在字符串循环中使用“+=”。由于它们是不可变的,因此您必须为循环中的每次迭代创建一个副本。对于小型集合,您可能不会注意到性能受到影响,但对于大型集合,这会对性能产生巨大影响。尝试使用 StringBuilder() 来构建字符串。 StringBuilder 效率更高。

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.

静赏你的温柔 2024-11-07 06:24:03

你需要去相关 += getRelatedNews( ... )

you need to go related += getRelatedNews( ... )

撩心不撩汉 2024-11-07 06:24:03

字符串是不可变的。您不能使用“+=”来更改字符串。

您将参数相关 设置为新字符串,但并未更改传入的字符串。

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.

月牙弯弯 2024-11-07 06:24:03

该问题与(等等)相关有关。你发生了递归。在第一个函数中,您递归地调用该函数,并且始终将结果连接到函数末尾的控件(在每次递归执行中到达)。在第二种情况下,您仍然在递归操作,但您从未捕获这些递归调用的结果。字符串是不可变的,相关不会被这些递归函数调用自动更新。开始捕获结果并对其进行评估,看看结果是否符合您的预期。

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.

征﹌骨岁月お 2024-11-07 06:24:03

函数的调用是什么样的?你在做这样的事情吗?

string blah = getRelatedNews(x, "", y);

如果是这样,请将其更改为:

string buffer,blah = getRelatedNews(x, buffer, y);

还将这一行更改

    getRelatedNews(cat, related, contentTitle);

    related += getRelatedNews(cat, related, contentTitle);

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);
复古式 2024-11-07 06:24:03

其实问题就在这里:

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);
 }

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);
 }
短暂陪伴 2024-11-07 06:24:03
// this code
     getRelatedNews(cat, related, contentTitle);
// changed to 
     related = getRelatedNews(cat, related, contentTitle);
// works
// this code
     getRelatedNews(cat, related, contentTitle);
// changed to 
     related = getRelatedNews(cat, related, contentTitle);
// works
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文