vb.net 显示数组/列表的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16054054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:50:18  来源:igfitidea点击:

Display content of the array/list

arraysvb.netlist

提问by Lucas

I'm looking for a way to display (in the MsgBoxor other read-able place) the array/list content, the listcontains only strings:

我正在寻找一种方法来显示(在MsgBox或其他可读的地方)数组/列表内容,list只包含字符串:

Dim list As New List(Of String)

Dim list As New List(Of String)

I would like to display the content of it in the MsgBoxfor now. I can convert the listto the arrayif there would be a need. Is there a way to do this?

我想暂时显示它的内容MsgBox。我可以转换listarray是否会有需要。有没有办法做到这一点?

回答by SysDragon

You can declare a variable and loop the array putting every element inside:

您可以声明一个变量并循环数组,将每个元素放入其中:

Dim sResult As String = ""

For Each elem As String In list
    sResult &= elem & " "
Next

Or you can use String.Join()to directly merge all the elements of the array (you will need to convert the Listto a normal array if using .NET framework before 4.0):

或者您可以使用String.Join()直接合并数组的所有元素(List如果使用 4.0 之前的 .NET 框架,则需要将其转换为普通数组):

Dim sResult As String = String.Join(", ", list.ToArray())

回答by Steve

How many elements are in that List? If they are few then a MessageBox could do

该列表中有多少个元素?如果它们很少,那么 MessageBox 可以做

Dim message = string.Join(Environment.NewLine, list.ToArray())
MessageBox.Show(message)

If there are many, then you need some kind of interface to display everything.
In this case you need at least a WinForm application with your user defined Form that contains a TextBox with its multiline property set to true.

如果有很多,那么您需要某种界面来显示所有内容。
在这种情况下,您至少需要一个 WinForm 应用程序,其中包含一个多行属性设置为 true 的 TextBox 的用户定义表单。

Dim message = string.Join(Environment.NewLine, list.ToArray())
textBox1.Text = message

Here a reference to MSDN docs on List(Of T)

这里参考了List(Of T) 上的 MSDN 文档