Kenny Bones
Kenny Bones

Reputation: 5129

VBA - Use multidimensional array instead of two separate ones?

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 Firstname in it's own array and FIRSTNAME in 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 Firstname in 0 and FIRSTNAME in 1. But how do I do that?

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(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 Each loop.

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

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

    'Run function strName, strValue
Next

Upvotes: 4

Views: 11308

Answers (1)

RB.
RB.

Reputation: 37192

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

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.

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

Upvotes: 4

Related Questions