VBA - 使用多维数组而不是两个单独的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5483537/
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-11 12:55:09  来源:igfitidea点击:

VBA - Use multidimensional array instead of two separate ones?

arraysvbavbscriptword-vba

提问by Kenny Bones

Lots of stupid questions lately, but I would appreciate some input on this one. I've got a string that comes from an INI-file. It looks like Firstname=FIRSTNAME. This is basically an array with loads of these. I want to split these up, but preserve both of them. So, I managed to put Firstnamein it's own array and FIRSTNAMEin it's own. But then a colleague of mine said "Why don't you just use a multidimensional array instead?". And this got me thinking, putting Firstnamein 0 and FIRSTNAMEin 1. But how do I do that?

最近有很多愚蠢的问题,但我希望能对此提出一些意见。我有一个来自 INI 文件的字符串。看起来像Firstname=FIRSTNAME。这基本上是一个包含大量这些的数组。我想把它们分开,但同时保留它们。所以,我设法放入Firstname了它自己的数组和FIRSTNAME它自己的数组。但是后来我的一位同事说“你为什么不直接使用多维数组呢?”。这让我开始思考,Firstname输入 0 和FIRSTNAME1。但是我该怎么做呢?

This is my code right now:

这是我现在的代码:

    For iTeller = 0 To UBound(arrIniName)
        If Not arrIniName(iTeller) = "" Then
            arrIniName(iTeller) = Split(arrIniName(iTeller), "=")(0)
        End If
    Next

    For iTeller = 0 To UBound(arrIniValue)
        If Not arrIniValue(iTeller) = "" Then
            arrIniValue(iTeller) = Split(arrIniValue(iTeller), "=")(1)
        End If
    Next

Both arrIniName and arrIniValues consists of the exact same array to begin with. Which looks like this:

arrIniName 和 arrIniValues 都由完全相同的数组开始。看起来像这样:

arrIniName(0) "Fistname=FIRSTNAME"
arrIniName(1) "Lastname=LASTNAME"
arrIniName(2) "Initials=INITIALS"

So I basically split each one into their own separate arrays the way I do it now. But putting them in a multi dimensional array would probably be better? Because then I'd have just one array to manage and could also pull that array through a For Eachloop.

所以我基本上按照我现在的方式将每个人分成自己单独的数组。但是将它们放在多维数组中可能会更好?因为那样我就只有一个数组要管理,并且还可以通过一个For Each循环将该数组拉出。

Edit: I ended up doing it like this, where Valuesis the array

编辑:我最终这样做了,Values数组在哪里

For Each s In Values
    Dim strName, strValue
    s = Split(s, "=")
    strName = s(0)
    strValue = s(1)

    'Run function strName, strValue
Next

采纳答案by RB.

The ideal solution sounds like a Dictionary(a data structure which holds Key/Value pairs - exactly what you have in your INI file).

理想的解决方案听起来像字典(一种包含键/值对的数据结构 - 正是您在 INI 文件中所拥有的)。

A multi-dimensional array would not be necessary here, as you only have 2 dimensions (key, and value). Arrays are generally more difficult to work with than dictionary's as they are hard to resize, so you need to know how many items you have upfront.

这里不需要多维数组,因为您只有 2 个维度(键和值)。数组通常比字典更难使用,因为它们难以调整大小,因此您需要预先知道有多少项。

Therefore, I would suggest the following code:

因此,我建议使用以下代码:

Dim dict As Dictionary
Set dict = new Dictionary
Dim key as String
Dim value as String

For iTeller = 0 To UBound(arrIniValue)
    If Not arrIniValue(iTeller) = "" Then
        key = Split(arrIniValue(iTeller), "=")(0)
        value = Split(arrIniValue(iTeller), "=")(1)
        dict.Add(key, value)
    End If
Next

However, if you want to use a multi-dimensional array, then the following will do it:

但是,如果您想使用多维数组,那么以下将做到:

' Declare a 2-dimensional array, of dimensions "n by 2".
Dim results(UBound(arrIniValue), 2) As String

For iTeller = 0 To UBound(arrIniValue)
    If Not arrIniValue(iTeller) = "" Then
        key = Split(arrIniValue(iTeller), "=")(0)
        value = Split(arrIniValue(iTeller), "=")(1)
        results(iTeller, 0) = key
        results(iTeller, 1) = value
    End If
Next