tags:

views:

59

answers:

2

I'm attempting to write a function to create a dictionary object such that the order of a given column in a given array will link to a key value in another dictionary that provides a locational value within another array (but that last part is outside of the scope)

Per the below code, I'm unclear as to why I would be receiving the error "The key is already associated with an element of this collection"

I have stepped through the code numerous times and the first pair added is where X and Y both = 0 ... so dict.Add X,Y adds Key = 0, KeyValue = 0

The next match is on X and Y both = 1 .. so dict.Add X,Y should add Key = 1, KeyValue = 1, but instead I am getting that error. I'm stumped!

(Although I'm expecting pairs like 0,0 : 1,1, : 2,2 etc... It's not certain so I need this code to be certain the code is dynamic and user friendly)

Any advice would be greatly appreciated!!

Function CreateDictionary(Array1, _
                     Array1_Index, _
                     Array1_no_of_dimensions, _
                     Array2, _
                     Array2_Index, _
                     Array2_no_of_dimensions)

Dim dict
Set dict = CreateObject("Scripting.Dictionary")
''creates an dictionary for arrays with a one-to-one relationship
''send index = 0 for single dimensional arrays
X = 0
Y = 0
dict.RemoveAll

If Array1_no_of_dimensions = 1 And Array2_no_of_dimensions = 1 Then
    While X <= UBound(Array1)
        While Y <= UBound(Array2)
            If Array1(X) = Array2(Y) Then
                Debug.Print dict.Item(1)
                '' When adding x = 1, y = 1 - this errors indicating "The key is already associated with an element of this collection"
                dict.Add X, Y
                Y = UBound(Array2)
            End If
            Y = Y + 1
        Wend
        Y = 0
        X = X + 1
        Debug.Print dict.Item(0)
    Wend
ElseIf Array1_no_of_dimensions = 1 And Array2_no_of_dimensions > 0 Then
    While X <= UBound(Array1)
        While Y <= UBound(Array2)
            If Array1(X) = Array2(Array2_Index, Y) Then
                dict.Add X, Y
                Y = UBound(Array2)
            End If
            Y = Y + 1
        Wend
        Y = 0
        X = X + 1
    Wend
ElseIf Array1_no_of_dimensions > 0 And Array2_no_of_dimensions = 0 Then
    While X <= UBound(Array1)
        While Y <= UBound(Array2)
            If Array1(Array1_Index, X) = Array1(Y) Then
                dict.Add X, Y
                Y = UBound(Array2)
            End If
            Y = Y + 1
        Wend
        Y = 0
        X = X + 1
    Wend
End If

CreateDictionary = dict

End Function
A: 

It appears using debug.print was initializing key values. Code still has an issue returning the dictionary object. Trying to find out why that is...will update with the new code if I get it working

A: 

Okay it appears I forgot a set command when calling the function:

e.g. dict1 = CreateDictionary(etc...)

should be : set dict1 = CreateDictionary(etc...)

Then in the code I also forgot to use SET when assigning the function equal to the dictionary object

Here is the updated code:

Function CreateDictionary(Array1, _
                     Array1_Index, _
                     Array1_no_of_dimensions, _
                     Array2, _
                     Array2_Index, _
                     Array2_no_of_dimensions)

Dim dict
Set dict = CreateObject("Scripting.Dictionary")
''creates an dictionary for arrays with a one-to-one relationship
''send index = 0 for single dimensional arrays
X = 0
Y = 0
dict.RemoveAll

If Array1_no_of_dimensions = 1 And Array2_no_of_dimensions = 1 Then
    While X <= UBound(Array1)
        While Y <= UBound(Array2)
            If Array1(X) = Array2(Y) Then
                dict.Add X, Y
                Y = UBound(Array2)
            End If
            Y = Y + 1
        Wend
        Y = 0
        X = X + 1
    Wend
ElseIf Array1_no_of_dimensions = 1 And Array2_no_of_dimensions > 0 Then
    While X <= UBound(Array1)
        While Y <= UBound(Array2)
            If Array1(X) = Array2(Array2_Index, Y) Then
                dict.Add X, Y
                Y = UBound(Array2)
            End If
            Y = Y + 1
        Wend
        Y = 0
        X = X + 1
    Wend
ElseIf Array1_no_of_dimensions > 0 And Array2_no_of_dimensions = 0 Then
    While X <= UBound(Array1)
        While Y <= UBound(Array2)
            If Array1(Array1_Index, X) = Array1(Y) Then
                dict.Add X, Y
                Y = UBound(Array2)
            End If
            Y = Y + 1
        Wend
        Y = 0
        X = X + 1
    Wend
End If

Set CreateDictionary = dict

End Function