32

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

7 Answers 7

47

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Can I access each item like Dim something as String = lstOfString(0) to get the String1 value?
@BrandonJ I just edited to show your access requirements - that get you what you want?
Thank you much for your help, but I'm still doing wrong when I try that, and getting a [value of type 'system.collections.generic.list(of String)' cannbot be converted to a 1-dimensional array of System.Colelctions.Generic.List(of String)'] I'm gonna edit my post and show my actual code
@BrandonJ It looks like your declaration is wrong - you probably have an extra (). See my declaration above for the Dim line...
41

You can do something like this,

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

Collection Initializers

1 Comment

"This is the one-liner you are looking for..."
4

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)

1 Comment

I like the extension method method.
3

look to the List AddRange method here

Comments

2

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

1 Comment

What about Dim blankList As New List(Of String) instead of yours?
0

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.

Comments

0

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"} )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.