[英]How to populate a dictionary with 2 split strings in VB 2017
I have 2 string, both split by (" ") and both around 40 different sub strings, first one called dick is the would-be keys, and the other one called dicv is the would-be values, how can I fill a dictionary with those? 我有2个字符串,都被(“”)分割,并且都有大约40个不同的子字符串,第一个叫dick的是可能的键,另一个叫dicv的是可能的值,我该如何填充字典和那些?
I tried this: 我尝试了这个:
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 is the string with keys and dicv the one with values. dick是带键的字符串,dicv是带值的字符串。
But I receive error "string cannot be converted to string()" when assigning dick and dicv as kvp.key and kvp.value. 但是当将dick和dicv分配为kvp.key和kvp.value时,我收到错误消息“字符串不能转换为string()”。
Thank you very much. 非常感谢你。
I am guessing from the error that dick and dicv are declared as arrays. 我从错误中猜测,dick和dicv被声明为数组。 The following is how you assign values to arrays.
以下是如何为数组分配值。 ArrayVariable(index) = value
ArrayVariable(index)=值
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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.