如何在VB 2017中使用2个拆分字符串填充字典

时间:2018-03-05 23:00:05

标签: vb.net dictionary split

我有2个字符串,两个字符串都被("")和大约40个不同的子字符串分开,第一个叫dick是可能的键,另一个叫做dicv就是是值,我怎么能用这些填写字典?

我试过了:

    Dim dicty As New Dictionary(Of String, String)

    For Each kvp As KeyValuePair(Of String, String) In dicty

    dick = kvp.Key
    dicv = kvp.Value

    Next

dick是带键的字符串,dicv是带值的字符串。

但我收到错误"字符串无法转换为字符串()"将dick和dicv指定为kvp.key和kvp.value。

非常感谢。

2 个答案:

答案 0 :(得分:0)

我猜错了dick和dicv被声明为数组。以下是如何为数组赋值。 ArrayVariable(index)= value

Private Sub ConvertDictionaryToArrays()
        Dim dicty As New Dictionary(Of String, String)
        Dim dick(dicty.Count - 1) As String
        Dim dicv(dicty.Count - 1) As String
        Dim index As Integer = 0
        For Each kvp As KeyValuePair(Of String, String) In dicty
            dick(index) = kvp.Key
            dicv(index) = kvp.Value
            index += 1
        Next
End Sub

答案 1 :(得分:0)

Private Sub FillDictionary()
    Dim dicty As New Dictionary(Of String, String)

    Dim dick(39) As String 'Guess that you have both of these arrays
    Dim dicv(39) As String

    For i As Integer = 0 To dick.Length - 1
        dicty.Add(dick(i), dicv(i))
    Next i
End Sub