List(of String) or Array or ArrayList

216,849

Solution 1

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

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.

Solution 2

You can do something like this,

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

Collection Initializers

Solution 3

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)

Solution 4

look to the List AddRange method here

Solution 5

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
Share:
216,849
Brandon
Author by

Brandon

I'm just trying to learn more about developing.

Updated on August 05, 2020

Comments

  • Brandon
    Brandon almost 4 years

    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).

    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