Call Let/Get Prop using string variable for name


Question

I need to call a Let or Get property using a string variable rather than the property's actual name because I'm using a generic class module as a go-between other class modules.

Answer


George,

If you don't mind to write

clsX.Test = clsB.Properties(strName)

instead of

clsX.Test = clsB.Co_Type

then I think you can use the following class as template for your solution:

Private Const mcstrClassName As String = "CPrps"

Dim mcolPrps As New VBA.Collection

Public Property Get Properties( _
       ByVal vstrPrpName As String) As Variant
  If IsObject(mcolPrps(vstrPrpName)) Then
    Set Properties = mcolPrps(vstrPrpName)
  Else
    Properties = mcolPrps(vstrPrpName)
  End If
End Property

Public Property Let Properties( _
       ByVal vstrPrpName As String, _
       ByRef vvar As Variant)
  On Error GoTo Properties_Err
  mcolPrps.Remove vstrPrpName
Properties_Err:
  On Error GoTo 0
  mcolPrps.Add vvar, vstrPrpName
End Property

And this is a test:

Public Sub A_Test()
  Dim prps As New CPrps
  prps.Properties("IntNum") = 1
  prps.Properties("String") = "two"
  prps.Properties("CodeDB") = CodeDb()
  prps.Properties("App") = Application
End Sub

HTH,
Shamil

 


Copyright 1999-2008 by Shamil Salakhetdinov.  All rights reserved.