D
Dreiding
I am trying to figure an easy way to access specific variables in a Class.
Consider having a Class module called "Phonebook" with Name and Address as
two of the variables. The Phonebook class also contains a “Fields†array
variable containing the desired variables names (Name, Address). The Class
definition is at the end of this message.
My sample code fails on
Application.Evaluate("myPhonebook." & vItem)
with Error 2029.
Any suggestions on making this work? Is there a better way?
Thanks, - Pat
Sample code
--------------------------------------------------------------------------------
Option Explicit
Sub TestPhonebook()
Dim myPhonebook As cPhonebook
Dim vItem As Variant
'initialize myPhonebook content
Set myPhonebook = New cPhonebook
myPhonebook.Name = "myName"
myPhonebook.Address = "MyAddress"
'display myPhonebook content
For Each vItem In myPhonebook.Fields
Debug.Print vItem, Application.Evaluate("myPhonebook." & vItem)
Next vItem
End Sub
--------------------------------------------------------------------------------
cPhonebook class
--------------------------------------------------------------------------------
Option Explicit
Private FName As String
Private FAddress As String
Private FFields As Variant
'FName
Public Property Let Name(ByVal Value As String)
FName = Value
End Property
'
Public Property Get Name() As String
Name = FName
End Property
'
'FAddress
Public Property Let Address(ByVal Value As String)
FAddress = Value
End Property
'
Public Property Get Address() As String
Address = FAddress
End Property
'
'FFields
Public Property Get Fields() As Variant
Fields = FFields
End Property
Private Sub Class_Initialize()
FFields = Array("Name", "Address")
End Sub
--------------------------------------------------------------------------------
Consider having a Class module called "Phonebook" with Name and Address as
two of the variables. The Phonebook class also contains a “Fields†array
variable containing the desired variables names (Name, Address). The Class
definition is at the end of this message.
My sample code fails on
Application.Evaluate("myPhonebook." & vItem)
with Error 2029.
Any suggestions on making this work? Is there a better way?
Thanks, - Pat
Sample code
--------------------------------------------------------------------------------
Option Explicit
Sub TestPhonebook()
Dim myPhonebook As cPhonebook
Dim vItem As Variant
'initialize myPhonebook content
Set myPhonebook = New cPhonebook
myPhonebook.Name = "myName"
myPhonebook.Address = "MyAddress"
'display myPhonebook content
For Each vItem In myPhonebook.Fields
Debug.Print vItem, Application.Evaluate("myPhonebook." & vItem)
Next vItem
End Sub
--------------------------------------------------------------------------------
cPhonebook class
--------------------------------------------------------------------------------
Option Explicit
Private FName As String
Private FAddress As String
Private FFields As Variant
'FName
Public Property Let Name(ByVal Value As String)
FName = Value
End Property
'
Public Property Get Name() As String
Name = FName
End Property
'
'FAddress
Public Property Let Address(ByVal Value As String)
FAddress = Value
End Property
'
Public Property Get Address() As String
Address = FAddress
End Property
'
'FFields
Public Property Get Fields() As Variant
Fields = FFields
End Property
Private Sub Class_Initialize()
FFields = Array("Name", "Address")
End Sub
--------------------------------------------------------------------------------