gpt4 book ai didi

c# - 想要使用一种方法从foreach返回列表并返回所有项目,但是我得到: (collection)

转载 作者:行者123 更新时间:2023-12-03 09:11:36 26 4
gpt4 key购买 nike

因此,基本上,当按钮上发生click事件以检查字符串是否匹配时,将触发该方法:

        lbPlaylist.Items.Add(o.GetFragment(tbTitle.Text));
}

该方法的代码为:
    public List<SoundFragment> GetFragment(String p)
{
List<SoundFragment> list1 = new List<SoundFragment>();

//make a list and store every string
foreach(SoundFragment i in fragments)
{
if (i.Title.Contains(p))
{
list1.Add(i);
}
}
//return all the objects from the new list that stores all the string contained in the fragments list
return list1;
}

问题是我真的将对象或对象集合返回到列表框,而我只想返回添加的字符串名称。我如何到达他们?

最佳答案

除了Richard的答案,您的主要问题是该行:

lbPlaylist.Items.Add(o.GetFragment(tbTitle.Text));

在这里,您将整个 集合作为一项添加到 ListBox中。你必须用
lbPlaylist.Items.AddRange(o.GetFragment(tbTitile.Text).ToArray());

将每个对象添加为单个项目。

然后, ListBox将使用集合中对象的 ToString()方法显示每个对象的文本。如果您的 SoundFragment实现了适合您需要的 ToString()方法,则一切正常。否则,您必须将 GetFragment方法修改为类似Richard建议的方法。

关于c# - 想要使用一种方法从foreach返回列表并返回所有项目,但是我得到: (collection),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34750223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com