vb.net List(of String) 或 Array 或 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13273997/
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:24:31  来源:igfitidea点击:

List(of String) or Array or ArrayList

vb.netlistcollections

提问by Brandon

Hopefully a simple question to most programmers with some experience.

希望对大多数有经验的程序员来说是一个简单的问题。

What is the datatype that lets me do this?

让我这样做的数据类型是什么?

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)

I would access these like this

我会像这样访问这些

Dim s1 = lstOfStrings(0)
Dim s2 = lstOfStrings(1) 
Dim s3 = lstOfStrings(2) 
Dim s4 = lstOfStrings(3)

if I use List(of String) I am only able to .add one thing to the list (at a time), and in my function I want to be able to store several values(at a time).

如果我使用 List(of String),我只能向列表中添加一件事(一次),并且在我的函数中,我希望能够(一次)存储多个值。

Solution:

解决方案:

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

回答by Reed Copsey

List(Of String)will handle that, mostly - though you need to either use AddRangeto add a collection of items, or Addto add one at a time:

List(Of String)将处理这个问题,主要是 - 尽管您需要使用AddRange来添加一组项目,或者Add一次添加一个:

lstOfString.Add(String1)
lstOfString.Add(String2)
lstOfString.Add(String3)
lstOfString.Add(String4)

If you're adding known values, as you show, a good option is to use something like:

如果您要添加已知值,如您所示,一个不错的选择是使用以下内容:

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)

This will still allow you to add items later as desired, but also get your initial values in quickly.

这仍然允许您稍后根据需要添加项目,但也可以快速获取初始值。



Edit:

编辑:

In your code, you need to fix the declaration. Change:

在您的代码中,您需要修复声明。改变:

Dim lstWriteBits() As List(Of String) 

To:

到:

Dim lstWriteBits As List(Of String) 

Currently, you're declaring an Array of List(Of String) objects.

目前,您正在声明一个 List(Of String) 对象数组。

回答by bluebunny72

You can do something like this,

你可以做这样的事情,

  Dim lstOfStrings As New List(Of String) From {"Value1", "Value2", "Value3"}

Collection Initializers

集合初始化器

回答by Guffa

Neither collection will let you add items that way.

这两个集合都不允许您以这种方式添加项目。

You can make an extension to make for examle List(Of String)have an Addmethod that can do that:

你可以做一个扩展,例如List(Of String)有一个Add可以做到这一点的方法:

Imports System.Runtime.CompilerServices
Module StringExtensions

  <Extension()>
  Public Sub Add(ByVal list As List(Of String), ParamArray values As String())
    For Each s As String In values
      list.Add(s)
    Next
  End Sub

End Module

Now you can add multiple value in one call:

现在您可以在一次调用中添加多个值:

Dim lstOfStrings as New List(Of String)
lstOfStrings.Add(String1, String2, String3, String4)

回答by Riccardo

look to the List AddRangemethod here

看看这里的 ListAddRange方法

回答by thedanotto

Sometimes I don't want to add items to a list when I instantiate it.

有时我不想在实例化列表时将项目添加到列表中。

Instantiate a blank list

实例化一个空白列表

Dim blankList As List(Of String) = New List(Of String)

Add to the list

添加到列表中

blankList.Add("Dis be part of me list") 'blankList is no longer blank, but you get the drift

Loop through the list

循环遍历列表

For Each item in blankList
  ' write code here, for example:
  Console.WriteLine(item)
Next

回答by Binny

You can use IList(Of String)in the function :

您可以IList(Of String)在函数中使用:

Private Function getWriteBits() As IList(Of String)


Dim temp1 As String
Dim temp2 As Boolean
Dim temp3 As Boolean


'Pallet Destination Unique
Dim temp4 As Boolean
Dim temp5 As Boolean
Dim temp6 As Boolean

Dim lstWriteBits As Ilist = {temp1, temp2, temp3, temp4, temp5, temp6}

Return lstWriteBits
End Function

use list1.AddRange(list2)to add lists

用于 list1.AddRange(list2)添加列表

Hope it helps.

希望能帮助到你。