3

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.

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:

FullName = String.Join(" ", Me.Get_NameList())

However, when I call:

Console.WriteLine(Person1.Print_Name())

I get what looks like the list object instead of the string:

System.Collections.Generic.List`1[System.String]

If I change the code to look like this:

    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

The console prints:

John Q Doe

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?

Thanks in advance for the help.

Here is the full 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
6
  • 3
    fyi - it's generally considered poor practice to have functions without explicit return types. Is suspect setting returns types on your functions will fix your problem, or at least force a compiler error that will make the problem more obvious. Oh, and you have either Option Infer or Option Explicit on, right? Commented Feb 27, 2012 at 15:43
  • 4
    Turn Option Strict On. Keeping it off can make it impossible to find problems. Commented Feb 27, 2012 at 15:45
  • Just a side note, but you would be better off just returning a concatenated string in Print_Name rather than getting a list and then joining it. Or using String.Format Commented Feb 27, 2012 at 15:53
  • @Joel: Why either ... or? The best option is to set Option Explicit and Option Infer to on. Commented Feb 27, 2012 at 16:20
  • Thanks for the info! I didn't even know about Explicit,Strict or Infer options. I have been teaching myself how to program and sometimes really basic things fall through the cracks! I haven't defined any options for the code, so I was using the default options. I guess I didn't notice because the VisualStudio IDE does some basic type checking for you. Oh and @musefan, I wanted the flexibility in future code to return a list or a string, so this saved me a little typing. If this is poor practice, let me know. Commented Feb 27, 2012 at 19:00

2 Answers 2

6

GetNameList returns an Object (because you don't specify the return type).

So the Join method is getting an object. So the VB.Net is turning the Object into a String() with one element that is Object.ToString(). Sometimes the method, especially if it is an old school VB holdover, would check to see if the object passed was an IEnumerable and just iterate over the Objects in the passed object. But not always. So having Strict and Explicit OFF can lead to very strange and hard to find bugs. Those two things should only be OFF in very specific cases where you want all the flexibility turning them off gives you AND you are ready to deal with the oddities that result.

Change the return type of Get_NameList to List(Of String)

And turn on option Strict ON and Option Explicit On to see your other problems.

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

1 Comment

I see. So I was just getting lucky when I defined the FullNameList as list(of string). The returned object was converted to a list at that point and then I was able concatenate it. Since I didn't declare the return type of the function originally, I was trying to join the object that hadn't been converted yet and that's why I was getting the object.
2

if you change this line:

Public Function Get_NameList()

to

Public Function Get_NameList() AS List(Of String)

And this line

Public Function Print_Name()

to

Public Function Print_Name() as string

it will work

2 Comments

Yes, and @Geoff, also change the name of the latter method. Print_Name sounds like the name of a Sub. The method actually does not print anything. Change it to GetPrintName or the like.
Yes it should be GetPrintName

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.