希望有一些简单的问题给大多数程序员有一些经验.
什么是允许我这样做的数据类型?
Dim lstOfStrings as *IDK* Dim String0 As String = "some value" Dim String1 As String = "some value" Dim String2 As String = "some value" Dim String3 As String = "some value" Dim String4 As String = "some value" Dim String5 As String = "some value" lstOfStrings.add(String0,String1,String2,String3)
我会这样访问这些
Dim s1 = lstOfStrings(0) Dim s2 = lstOfStrings(1) Dim s3 = lstOfStrings(2) Dim s4 = lstOfStrings(3)
如果我使用List(String)
我只能将一个东西添加到列表中(在某个时间),在我的函数中,我想要能够存储几个值(一次).
解:
Private Function Foo() As List(Of String) Dim temp1 As String Dim temp2 As String Dim temp3 As String Dim temp4 As String Dim temp5 As String Dim temp6 As String Dim inputs() As String = {temp1,temp2,temp3,temp4,temp5,temp6} Dim lstWriteBits As List(Of String) = New List(Of String)(inputs) Return lstWriteBits End Function
List(Of String)将主要处理,尽管您需要使用AddRange添加项目集合,或者添加一次添加一个:
原文链接:https://www.f2er.com/vb/255651.htmllstOfString.Add(String1) lstOfString.Add(String2) lstOfString.Add(String3) lstOfString.Add(String4)
如果你正在添加已知的值,那么一个很好的选择是使用像
Dim inputs() As String = { "some value",_ "some value2",_ "some value3",_ "some value4" } Dim lstOfString as List(Of String) = new List(Of String)(inputs) ' ... Dim s3 = lstOfStrings(3)
这将仍然允许您根据需要稍后添加项目,但也可以快速获取初始值.
编辑:
在您的代码中,您需要修复声明.更改:
Dim lstWriteBits() As List(Of String)
至:
Dim lstWriteBits As List(Of String)
目前,您正在声明一个List(String)对象的数组.