How to convert a hashtable to string array in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections

Module HashTableToStringArray

    ' Function to convert a Hashtable into a string array of "key:value" pairs
    Public Function ToPairArray(ht As Hashtable) As String()
        Dim pairArray(ht.Count - 1) As String
        Dim index As Integer = 0
        
		For Each entry As DictionaryEntry In ht
            pairArray(index) = $"{entry.Key}:{entry.Value}"
            index += 1
        Next
	
        Return pairArray
    End Function

    Sub Main()
        Dim ht As New Hashtable()

        ht.Add("A", "C")
        ht.Add("B", "Java")
        ht.Add("C", "Python")
        ht.Add("D", "Go")
        ht.Add("E", "Rust")
        ht.Add("F", "TypeScript")

        Dim pairArray As String() = ToPairArray(ht)

        Console.WriteLine("Key-Value Pairs: " & String.Join(", ", pairArray))
    End Sub

End Module

	
' run:
'
' Key-Value Pairs: E:Rust, F:TypeScript, A:C, D:Go, C:Python, B:Java
' 

 



answered Dec 4, 2025 by avibootz
...