How To Initialize A List Of String-Arrays

Cator Canulis 21 Reputation points
2021-02-26T08:55:00.887+00:00

Hello everyone,

I have a function f that takes a list of String-Arrays ( List(Of String()) ) as an argument.

Right now I'm passing it to the function like this:

Dim string1 As String() = {"foo", "bar"}
Dim string2 As String() = {"further", "words"}

Dim loS = New System.Collections.Generic.List(Of String()) From {string1, string2}

f(loS)

This seems unnecessarily complicated. But when I try to do it like this:

f( { {"foo", "bar"}, {"further", "words"} } )

I get an error like:

2-dimensional Array of String cannot be converted to System.Collections.Generic.List()

What am I doing wrong? How can I initialize an Object of type List(Of String())?

Thanks in davance

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,386 Reputation points
    2021-02-26T13:48:41.11+00:00

    Hello,

    Here are two methods to work with

    Module Program  
        Sub Main(args As String())  
            Example1()  
            Console.WriteLine()  
            Example2()  
            Console.ReadLine()  
        End Sub  
        Private Sub Example1()  
            Dim myList As New List(Of String()) From {  
                    New String() {"foo", "bar"},  
                    New String() {"further", "words"}}  
      
            Console.WriteLine("Example1")  
      
            For Each strings As String() In myList  
                Console.WriteLine(String.Join(",", strings))  
            Next  
      
        End Sub  
      
        Private Sub Example2()  
      
            Dim string1 As String() = {"foo", "bar"}  
            Dim string2 As String() = {"further", "words"}  
      
            Dim myList As New List(Of String()) From {string1, string2}  
      
            Console.WriteLine("Example2")  
      
            For Each strings As String() In myList  
                Console.WriteLine(String.Join(",", strings))  
            Next  
        End Sub  
    End Module  
      
    

    72438-11111111111.png

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Viorel 114.7K Reputation points
    2021-02-26T09:20:16+00:00

    Try a longer way:

    f2( { ({"foo", "bar"}), ({"further", "words"}) } )
    

    where f2 is a helper function:

    Sub f2(p()() As String)
        f(p.ToList)
    End Sub
    

    Or, if possible, change the parameter type of f to call it directly.

    1 person found this answer helpful.
    0 comments No comments

  2. Cator Canulis 21 Reputation points
    2021-02-26T21:01:13.047+00:00

    Can confirm that both solutions work. Thank you! I picked the second one, because it's better suited for my particular problem. A helper function can however be a very elegant solution.

    0 comments No comments