Nick Thalken
Nick Thalken

Reputation: 1

CallByName for collection of created class

So I have created the code below which is a function that takes in two different variables of the CReliability class (self made class in a class module). It also takes in two different collections that each are apart of. This function is designed to assign all unique items within each property of the rawRel variable (CReliability type) to the newRel variable (CReliability Type) within the newCol (Collection) and also count how many times each unique item occurs for each property. I would like to expand this idea so that I can implement this function for any property I would like to access, get unique items, and count. I am having issue using the VBA function CallByName to do this however. The line I am having issue with is only:

newRel.defect = curr 'This works but is not desired since it is a specific property
CallByName(newRel, "defect", VbGet) = curr 'This does not work and I want to work so I can use any property

which is apart of the entire function:

Sub uniqueArr(ByRef newRel, ByRef rawRel, ByRef relCol, ByRef newCol, property As String)
For Each rawRel In relCol
curr = CallByName(rawRel, "defect", VbGet)
bool = False
For Each newRel In newCol
    If CallByName(newRel, "defect", VbGet) = curr Then
        bool = True
    End If
Next
If Not bool Then
    Set newRel = New CReliability
    newRel.defect = curr 'This works but is not desired since it is a specific property
    CallByName(newRel, "defect", VbGet) = curr 'This does not work and I want to work so I can use any property
    newCol.Add newRel
End If
Next
For Each newRel In newCol
    If newRel.defect = rawRel.defect Then
        newRel.defectCount = newRel.defectCount + 1
    End If
Next
End Sub

If anyone could help me understand why the CallByName function does not work in this instance that would be great. I can also post the CReliability class code if that's useful.

Upvotes: 0

Views: 564

Answers (1)

KekuSemau
KekuSemau

Reputation: 6853

You can use

CallByName(newRel, "defect", VbGet)

to get the value, but you can obviously not use this as an LValue (ie assign something to it).

Try

CallByName newRel, "defect", VbLet, curr

Upvotes: 3

Related Questions