• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

dynamically accessing properties of a object


I want the following:

function here(a as object,id as string) as object
  here = a.id
end function

id does not have to be a string, but it needs to be generic.  That is I don't want to have to check types for id.
1 Solution
You can use the CallByName function...



property = CallByName(a, "id", vbGet)
rmundkowskyAuthor Commented:
Right now, thanks!

Public Function find(thingToSearch As Collection, keyToFind As Variant, attributeToMatch As String) As Collection
    Dim temp As Object
    Dim tempCollection As Collection
    Dim varB

    Set tempCollection = New Collection
    For i = 1 To thingToSearch.Count
        Set temp = thingToSearch.Item(i)

        varB = CallByName(temp, attributeToMatch, VbGet)
        'if typeof temp is VBA.
        'changing attributeToMatch from a string is hard, but I think it can be done
        '  also we need to have multiple if then's for
        If StrComp(TypeName(varB), "String") = 0 Then
            If StrComp(varB, keyToFind) = 0 Then
                    Set find = tempCollection
                    tempCollection.Add temp
            End If
            If keyToFind = varB Then
                Set find = tempCollection
                tempCollection.Add temp
            End If
        End If

    If tempCollection.Count = 0 Then
        Set find = Nothing
    End If

End Function

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now