[Webinar] Streamline your web hosting managementRegister Today

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

dynamically accessing properties of a object

Hi,

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.
0
rmundkowsky
Asked:
rmundkowsky
1 Solution
 
Erick37Commented:
You can use the CallByName function...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctcallbynamefunction.asp

e.g.

property = CallByName(a, "id", vbGet)
0
 
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
        Else
            If keyToFind = varB Then
                Set find = tempCollection
                tempCollection.Add temp
            End If
        End If
    Next

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

End Function


0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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