List(of String) or Array or ArrayList

后端 未结 7 2030
借酒劲吻你
借酒劲吻你 2021-01-01 10:27

Hopefully a simple question to most programmers with some experience.

What is the datatype that lets me do this?

Dim lstOfStrings as *IDK*

Dim Stri         


        
相关标签:
7条回答
  • 2021-01-01 10:54

    You can do something like this,

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

    Collection Initializers

    0 讨论(0)
  • 2021-01-01 10:54

    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
    
    0 讨论(0)
  • 2021-01-01 10:55

    Neither collection will let you add items that way.

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

    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)
    
    0 讨论(0)
  • 2021-01-01 10:55

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

    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

    Hope it helps.

    0 讨论(0)
  • 2021-01-01 10:57

    For those who are stuck maintaining old .net, here is one that works in .net framework 2.x:

    Dim lstOfStrings As New List(of String)( new String(){"v1","v2","v3"} )
    
    0 讨论(0)
  • 2021-01-01 11:06

    look to the List AddRange method here

    0 讨论(0)
提交回复
热议问题