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
Git
Gist
- CollectionDisplayContents
Link
Dedicate
Dedicating to:-
- string.Join(string, string[]) returns “System.String[]”
Link - Schabse Laks ( SLaks )
- Developer, C# MVP (2010 – 2018)
- Profile
Link
- Neils Keurentjes
- Profile
Link
- Profile
- DStanley
- Profile
Link
- Profile
Schabse Laks ( SLaks )
DStanley
Summary
The joys and headache of method overloading.