List the properties of a class in VBA 2003

前端 未结 1 1048
时光取名叫无心
时光取名叫无心 2021-01-21 12:48

I\'ve searched all over to see if there is an easy answer to this question, but there doesn\'t seem to be...

I\'m using Excel VBA 2003 (yes, I know it\'s out-of date, bu

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-21 13:12

    As John mentions above, reflection is not supported in VBA. Here is a hack that I have used before. Basically you can create a Collection or Dictionary object to store your "properties" by name.

    Option Explicit
    
    Private pProperties As Object
    
    Public Property Get Properties() As Object
        Set Properties=pProperties
    End Property
    
    Public Property Let Properties(p as Object) 
        Set pProperties = p
    End Property
    
    
    Sub Class_Initialize()
        Set pProperties = CreateObject("Scripting.Dictionary")
    
        'Add/instantiate your properties here
        pProperties("foo") = "this is foo"
        pProperties("bar") = "this is bar"
    
    
    End Sub
    

    Calling code

    Dim myFooBar As New cFooBar, P As Variant
    
    For Each P In myFooBar.Properties.Keys()
        Debug.Print P, myFooBar.Properties(P)
    Next
    

    0 讨论(0)
提交回复
热议问题