【问题标题】:Assign value to array为数组赋值
【发布时间】:2020-05-26 01:29:52
【问题描述】:

我有一个dictionary,它还存储了array。这是代码,我正在尝试为数组分配一些值。但它总是无法为array 赋值。我该如何解决这个问题?谢谢

代码:

Dim dict As Dictionary
Set dict = New Dictionary
For i = 1 To fruitList.count
    dict .Add fruitList(i), Array(0, 0, 0, 0, 0)
next
dict(apple)(0) = 100 //however, the first index in the array always equals to 0

更新: 或者我应该这样问。我在dictionary 中添加array 作为值的方式不是错误的吗?

【问题讨论】:

  • 您是否尝试在 VB.NET 中执行此操作?还是VBA?你的标签是 VBA。
  • @braX excel vba

标签: arrays vba dictionary


【解决方案1】:

试试这个:

Sub addArray2Dict()
    Dim a: a = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) 'Here declare and store the array,
                                                   'you can do it any other way
                                                   'there is no difference if you do it here or
                                                   'in the loop
    Dim b 'Just for testing porpuse
    Dim oDict As Object 'Because I use “Microsoft Scripting Runtime” can use earle binding
    Set oDict = CreateObject("Scripting.Dictionary")
    Dim i 'iterator

    'Store numbers and letters inside the dictionary
    'BUT in the index number 5 I will store the array
    For i = 1 To 20
        If i = 5 Then
            oDict.Add i, a
           'oDict.Add i, Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) 'As I said, this is the same as the line above.
        Else
            oDict.Add i, Chr(66 + i) 'Storing just letters in every key(integer)
        End If
    Next i

    b = oDict(5) 'Here I take the array from the Dictionary, and store it inside b,
    Debug.Print b(3) 'Here just print the 3th (number 4)  elemente of the array
                     'remember the array is:
                     '
                     ' array( 1 2 3 4 5 6 7 8 9 0 ) right!
                     '        0 1 2 3 4 5 6 7 8 9   <<< with that index
                     '
                     ' if i want the 3th value i will get the number 4 in my example.
End Sub

Use this as a reference

“Microsoft Scripting Runtime”(使用工具添加->来自 VB 的引用 菜单)

据我了解,您的代码可能是这样的:

Sub Test()
    Dim fruitList(1 To 5) As String
    Dim i
    Dim B
    fruitList(1) = "apple"
    fruitList(2) = "banana"
    fruitList(3) = "melon"
    fruitList(4) = "orange" 'Love oranges!
    fruitList(5) = "kiwi" 'Love Kiwi too!!!!
    Dim dict As Dictionary 
    Set dict = New Dictionary 
    For i = LBound(fruitList) To UBound(fruitList) 'here you use the bounds of the array,
                                                   'instead .count, here won't work
        dict.Add fruitList(i), Array(0, 0, 0, 0, 0)
    Next
    B = dict("apple") '(0) = 100 'however, the first index in the array always equals to 0
    B(0) = 100 'Here! You take the array FROM the dictionary and store it inside B
               'Just do it!
    B(1) = 200
    B(2) = 300
    B(3) = 500
    B(4) = 1000000
    Debug.Print B(0) 'Testing! This will print in the console your value (100).
    dict.Remove "apple" 'Remove the array
    dict.Add "apple", B 'Return the new array!
End Sub

您不能更改存储在字典中的数组的值(在 VBA 中)。最好删除数组并更改值,然后,如果需要,将其存储回字典中。

检查this answer 如果Tim Williams 说你不能,那是因为没有人可以!

【讨论】:

  • 感谢您的简短解释。但是,如何为 b(3) 赋值?因为我的情况是当我做 oDict(5)(3) = 10, b(3) 仍然返回 4
  • 好的,你的代码可以工作了。您需要从字典中获取数组,检查这个答案:Link
  • 和!如果这对您有帮助,请...设置答案!请发送一些爱!
  • 只需更改一些 cmets 来修正你的代码。
【解决方案2】:

都可以在这里找到:https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/

Dim myArray(arraySize) As ArrayType

Dim myArray = New ArrayType() {Item1, Item2, ...}

因此,在实践中,它应该如下所示:

Dim companies(3) as String
companies(0) = "Microsoft"
companies(1) = "Google"
companies(2) = "Amazon"

或:

Dim companies = New String() {"Microsoft", "Google", "Amazon"}

【讨论】:

  • 感谢您的回复,但是如何在字典中添加数组作为值?
  • 请注意,这是.Net 链接(标签是VBA,而不是Vb.net)。
  • 这可以在这里找到 vb.net-informations.com/collections/dictionary.htm Dim dict As New Dictionary(Of KeyType, ValType)() 在实践中,它会是:Dim compAndCEO As New Dictionary(Of String, String)() compAndCEO.add("Amazon", "Jeff Bezos")
  • @WILLIAM 如果这解决了您的问题,您能否将我的回复标记为答案?
  • 这不适用于 VBA。 VBA 与 VB6 或 VB.NET 不同
猜你喜欢
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多