什么是最好的 VBA 数据类型`key`=>`value` 来保存与 PHP 数组相同的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11463799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:31:30  来源:igfitidea点击:

What is the best VBA data type`key`=>`value` to save data same as PHP array

arrayslistvbakey-value

提问by Davuz

I'm working with VBA and need to save data in type key=>valueto getting fastest; This data type help me cache responese text from http request, increase query speed. But I don't know what is the best way to do it? I need a data type same as php array with key=>value! Thank for help!

我正在使用 VBA,需要将数据保存在类型key=> 中value才能获得最快的速度;这种数据类型帮助我缓存来自 http 请求的响应文本,提高查询速度。但我不知道最好的方法是什么?我需要一个与 php 数组相同的数据类型key=>value!感谢帮助!

回答by Irfarino

Have you looked at dictionary object?

你看过字典对象吗?

It's available as part of the Microsoft Scripting Runtime. A clear example of how to add this is given by this SO answer.

它作为 Microsoft Scripting Runtime 的一部分提供。这个 SO answer给出了一个关于如何添加它的清晰示例。

Sub DictExample1()

Dim dict As Dictionary
Dim v As Variant

    'Create the dictionary          
    Set dict = New Dictionary

   'Add some (key, value) pairs
    dict.Add "John", 34
    dict.Add "Jane", 42
    dict.Add "Ted", 402

    'How many items do we have?
    Debug.Print "Number of items stored: " & dict.Count

    'We can retrieve an item based on the key
    Debug.Print "Ted is " & dict.Item("Ted") & " years old"


   'We can test whether an item exists
    Debug.Print "We have Jane's age: " & dict.Exists("Jane")
    Debug.Print "We have Zak's age " & dict.Exists("Zak")

    'We can update a value by replacing it
   dict.Item("Ted") = dict.Item("Ted") / 10

    Debug.Print "Ted's real age is: " & dict.Item("Ted")

   'We can add more items
    dict.Add "Carla", 23

   'And we can iterate through the complete dictionary
    For Each v In dict.Keys
        Debug.Print "Name: " & v & "Age: "; dict.Item(v)
    Next

End Sub

(Source: http://www.techbookreport.com/tutorials/vba_dictionary.html)

(来源:http: //www.techbookreport.com/tutorials/vba_dictionary.html