Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does string.join return list object in VB.Net
    primarykey
    data
    text
    <p>I am having trouble understanding the difference between these two commands that in my mind should do the same thing. I have posted the entire code below in case anything is unclear.</p> <p>I have created two functions in class Person, one that returns a list containing first,middle and last names and one that returns a concatenated string of the name. I reference the function that returns the list to concatenate the string with the line below:</p> <pre><code>FullName = String.Join(" ", Me.Get_NameList()) </code></pre> <p>However, when I call:</p> <pre><code>Console.WriteLine(Person1.Print_Name()) </code></pre> <p>I get what looks like the list object instead of the string:</p> <pre><code>System.Collections.Generic.List`1[System.String] </code></pre> <p>If I change the code to look like this:</p> <pre><code> Public Function Print_Name() Dim FullNameList As List(Of String) = Me.Get_NameList() Dim FullName As String FullName = String.Join(" ", FullNameList) Return FullName End Function </code></pre> <p>The console prints:</p> <pre><code>John Q Doe </code></pre> <p>Why am I getting a different answer by first assigning the list to a variable and then joining it? Does this have something to do with how the list is stored in memory? </p> <p>Thanks in advance for the help.</p> <p>Here is the full code:</p> <pre><code>Imports System Module Module1 Sub Main() Dim Person1 As New Person("John", "Q", "Doe") Console.WriteLine("Get_Name Values") Dim g1 As List(Of String) = Person1.Get_NameList() Console.WriteLine(String.Join(" ", g1)) Console.WriteLine("Print_Name Values") Console.WriteLine(Person1.Print_Name()) End Sub End Module Class Person Private FirstName As String Private MiddleName As String Private LastName As String Public Sub New(ByVal Fn As String, ByVal Mn As String, ByVal Ln As String) FirstName = Fn MiddleName = Mn LastName = Ln End Sub Public Function Get_NameList() Dim NameList As New List(Of String) NameList.Add(FirstName) NameList.Add(MiddleName) NameList.Add(LastName) Return NameList End Function Public Function Print_Name() 'Dim FullNameList As List(Of String) = Me.Get_NameList() Dim FullName As String FullName = String.Join(" ", Me.Get_NameList()) Return FullName End Function End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload