user1215847
user1215847

Reputation: 1

VBA - Accessing object properties via another object (property set)

New to building class modules in VBA and struggling with populating one object which is defined within another object.

For instance, I have two class modules, Class1 and Class2

Class1

Dim mobj As Class2
Public Property Set SetObj(obj As Class2)
     Set mobj = obj
End Property

Class2

Public FirstName As String

These are accessed from a standard module as follows:

Sub test()
Dim X As Class1
Set X = New Class1
Set X.SetObj = New Class2
X.SetObj.FirstName = "Bruce"
End Sub

This however fails in X.SetObj.FirstName = "Bruce" when i get an "Invalid use of property message". Any assistance would be greatly appreciated.

Upvotes: 0

Views: 3027

Answers (1)

assylias
assylias

Reputation: 328568

You are close. Class1:

Private mobj As Class2
Public Property Set Obj(Obj As Class2)
     Set mobj = Obj
End Property

Public Property Get Obj()
     Set Obj = mobj
End Property

Sub (the X.Obj.FirstName line calls the Get property, not the Set property):

Sub test()
  Dim X As Class1
  Set X = New Class1
  Set X.Obj = New Class2
  X.Obj.FirstName = "Bruce"
End Sub

Upvotes: 3

Related Questions