集合变量问题

发布于 2024-10-15 23:29:52 字数 305 浏览 2 评论 0原文

假设我有一个字符串格式的通用集合。我想将这些值添加到标签中,我将如何去做,我尝试了一些我在这里读到的东西,但似乎无法让它发挥作用。

List<string> listcollection= new List<string>();

....Populate Collection Here....

MsgLabel.Text = Controls[string.Format(("MyInts: {0}", listcollection[1].Text));

任何帮助都会很棒。

谢谢。

Lets say that I have a generic collection in the string format. I want to extra the values to a label how would I go about doing that ive tried a few things ive read on here but cant seem to get it to work.

List<string> listcollection= new List<string>();

....Populate Collection Here....

MsgLabel.Text = Controls[string.Format(("MyInts: {0}", listcollection[1].Text));

Any help would be awesome.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

痴情 2024-10-22 23:29:52

有多种方法可以理解您的问题:

  • 您想要提取第二个值并将其放置在标签中,如示例所示
  • 您想要将所有值组合到一个列表中(MyInts 是复数)

提取第二个值

MsgLabel.Text = string.Format("MyInts: {0}", listcollection[1]);

将它们组合起来

您'您可能正在寻找 string.Join

这适用于您发布的示例:

MsgLabel.Text = string.Format("MyInts: {0}", string.Join(", ", listcollection));

该代码需要 .NET 4.0,否则 string.Join 需要一个数组,因此如果您不在 4.0 上,则需要以下代码:

MsgLabel.Text = string.Format("MyInts: {0}", string.Join(", ", listcollection.ToArray()));

There's multiple ways to understand your question:

  • You want to extract the 2nd value and place it in the label, as shown in the example
  • You want to combine all the values into a list (MyInts is plural)

Extract 2nd value

MsgLabel.Text = string.Format("MyInts: {0}", listcollection[1]);

To combine them

You're probably looking for string.Join.

This would work with the example you've posted:

MsgLabel.Text = string.Format("MyInts: {0}", string.Join(", ", listcollection));

That code requires .NET 4.0, otherwise string.Join requires an array, so if you're not on 4.0, the following code is what you need:

MsgLabel.Text = string.Format("MyInts: {0}", string.Join(", ", listcollection.ToArray()));
浅唱々樱花落 2024-10-22 23:29:52

如果您要创建通用字符串集合,则不应调用“Text”属性 collection[idx].Text。只需使用 .Join 或从集合中提取特定元素即可。

If you are creating generic collection of strings you should not call "Text" property collection[idx].Text. Just use .Join or extract particular element from the collection.

天邊彩虹 2024-10-22 23:29:52

如果你想组合所有字符串(在我看来你想这样做),你可以这样做:

List<string> listcollection = new List<string>();
string myText = string.Empty;
for (int i = 0; i < listcollection.Count; i++) {
   myText += ("string no. " + (i - 1).ToString() + ": " + listcollection[i] + Environment.NewLine);
}
MsgLabel.Text = myText;

或者使用 , 而不是 Environment.NewLine if你想用 , 分割它

编辑:请参阅 Lasse V. Karlsen 的评论以获得更快的解决方案。谢谢你的提示!

If you want to combine all the strings (and it seems to me like you wanna do that), you can do something like:

List<string> listcollection = new List<string>();
string myText = string.Empty;
for (int i = 0; i < listcollection.Count; i++) {
   myText += ("string no. " + (i - 1).ToString() + ": " + listcollection[i] + Environment.NewLine);
}
MsgLabel.Text = myText;

or use , instead of Environment.NewLine if you want to split it by ,

EDIT: See the comment of Lasse V. Karlsen for a quicker solution. Thanks for the hint!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文