.Net :- String.Join returns “System.String[]”

Background

Hit a curb yesterday and wanted to share about it today.

I have a collection and attempted to display its contents.

 

Code

Original


Dim listofFruit As ArrayList = New ArrayList
            
listofFruit.Add("Apple")
listofFruit.Add("Orange")
listofFruit.Add("Pear")

listManagement(listofFruit)

Private Sub listManagement(listofFruit As Object)
            
    Const FORMAT_DISPLAY As String = "{0, -40}  {1, -30}  {2,-30}"
    
    Dim arrayofFruit As Array = Nothing
    Dim strFruitsFromStringJoin as String = ""
    Dim strFruitsFromArrayToString as String = ""
    Dim strLog as String = ""

    arrayofFruit = listofFruit.ToArray()
    
    strFruitsFromStringJoin = String.Join(", ",  arrayofFruit)
    
    strfruitsFromArrayToString = arrayofFruit.ToString()
    
    strLog = String.Format(FORMAT_DISPLAY _
                             , "listofFruit" _
                             , listofFruit.GetType() _
                             , listofFruit.ToString())
    
    Console.WriteLine(strLog)
    
    strLog = String.Format(FORMAT_DISPLAY _
                             , "arrayofFruit" _
                             , arrayofFruit.GetType() _
                             , arrayofFruit)
    
    Console.WriteLine(strLog)
    
    strLog = String.Format( _
                            FORMAT_DISPLAY _ 
                            , "strFruitsFromStringJoin" _
                            , strFruitsFromStringJoin.GetType() _
                            , strFruitsFromStringJoin)
    
    Console.WriteLine(strLog)
    
    strLog = String.Format( _
                            FORMAT_DISPLAY _
                            , "strfruits" _
                            , strFruitsFromArrayToString.GetType() _
                            , strFruitsFromArrayToString)
    
    Console.WriteLine(strLog)
    
End Sub 

Output

Image

 

Revision


Dim collectionofFruit As New List(Of String)

collectionofFruit.Add("Apple")
collectionofFruit.Add("Orange")
collectionofFruit.Add("Pear")

Call listManagementWithObjectString(collectionofFruit )


Private Sub listManagementWithObjectString(listofFruit As List(Of String))
	
	Dim arrayofFruit() As String
	Dim strFruitsFromStringJoin as String = ""
	Dim strFruitsFromObjectToString as String = ""
	Dim strFruitsFromArrayToString as String = ""
	Dim strLog as String = ""
	
	Const FORMAT_DISPLAY As String = "{0, -40}  {1, -30}  {2,-30}"
		
	'Convert List to array    
	arrayofFruit = listofFruit.ToArray()
	
	'Join array contents using comma (,)
	strFruitsFromStringJoin = String.Join(", ",  arrayofFruit)
	
	'Issue ToString against Array of String
	strFruitsFromObjectToString = arrayofFruit.ToString()
	
	strLog = String.Format(FORMAT_DISPLAY _
				 , "strFruitsFromObjectToString" _
				 , strFruitsFromObjectToString.GetType() _
				 , strFruitsFromObjectToString)
	
	Console.WriteLine(strLog)
	
	strLog = String.Format( _
				    FORMAT_DISPLAY _ 
				  , "strFruitsFromStringJoin" _
				  , strFruitsFromStringJoin.GetType() _
				  , strFruitsFromStringJoin)
	
	Console.WriteLine(strLog)
	
	strLog = String.Format( _
				   FORMAT_DISPLAY _
				 , "String.Join(', ', listofFruit.ToArray)" _
				 , "" _
				 , String.Join(", ", listofFruit.ToArray))
				
	Console.WriteLine(strLog)

End Sub


Output

Image

Sharing

Restester.com

Links

  1. Visual Basic Online Compiler
    Link
  2. Vb.Net – Displaying Collection Contents
    Link

Git

Gist

  1. CollectionDisplayContents
    Link

Dedicate

Dedicating to:-

  1. string.Join(string, string[]) returns “System.String[]”
    Link
  2. Schabse Laks ( SLaks )
    • Developer, C# MVP (2010 – 2018)
    • Profile
      Link
  3. Neils Keurentjes
  4. DStanley

Schabse Laks ( SLaks )

DStanley

Summary

The joys and headache of method overloading.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s