集合变量问题
假设我有一个字符串格式的通用集合。我想将这些值添加到标签中,我将如何去做,我尝试了一些我在这里读到的东西,但似乎无法让它发挥作用。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有多种方法可以理解您的问题:
提取第二个值
将它们组合起来
您'您可能正在寻找
string.Join
。这适用于您发布的示例:
该代码需要 .NET 4.0,否则
string.Join
需要一个数组,因此如果您不在 4.0 上,则需要以下代码:There's multiple ways to understand your question:
Extract 2nd value
To combine them
You're probably looking for
string.Join
.This would work with the example you've posted:
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:如果您要创建通用字符串集合,则不应调用“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.
如果你想组合所有字符串(在我看来你想这样做),你可以这样做:
或者使用
,
而不是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:
or use
,
instead ofEnvironment.NewLine
if you want to split it by,
EDIT: See the comment of Lasse V. Karlsen for a quicker solution. Thanks for the hint!