2

I'm trying to construct a type within a type using Class Modules.

I have a standard code module with my Types, and a Class Module with Set/Let/Get Properties for each type. I then have one as an example of how I'm trying to use them.

This method works with one type, but not with a type within a type. When setting the second type in the standard module, I get a

"Compile error: Method or data member not found".


Class module 1, called clsMod

Option Explicit
Private Memento As clsMo2

Friend Sub SetMemento(NewMemento As clsMo2)
    Memento = NewMemento
End Sub

Friend Property Set clsTes2(value As clsMo2)
    Memento.m_clsTes2 = value 'This is where it errors, with "m_clsTes2" highlighted
End Property
Friend Property Get clsTes2() As clsMo2
    clsTes2 = Memento.m_clsTes2
End Property

Class Module 2, called clsMo2

Option Explicit
Private Memento2 As MyTyp2
Friend Sub SetMemento2(NewMemento As MyTyp2)
        Memento2 = NewMemento
End Sub

Public Property Let stri(value As String)
    Memento2.stri = value
End Property
Public Property Get stri() As String
    stri = Memento2.stri
End Property

Standard code module with Types

Type MyTyp2
    stri      As String
End Type

Type MyType
    m_clsTes2 As MyTyp2
End Type

Standard code module with Sub

Option Explicit
Public clsTest As clsMod
Public clsTes2  As clsMo2

Public Sub Variables()
    Set clsTest = New clsMod
    Set clsTest.clsTes2 = New clsMo2 'This line triggers the error
    clsTest.clsTes2.stri = "TestString" 'This is what I want to get to be able to do
End Sub
4
  • There is no member variable or method called m_clsTes2 in your clsMo2 class Commented Nov 25, 2019 at 17:29
  • 2
    You do NOT want to expose public user-defined types like this. That said in order for clsTest to have a clsTes2 member, the clsMod class would need to expose a Public Property Get clsTes2() As clsMod2. Protip: drop the Hungarian Notation prefixing scheme, it's really not helping here. Commented Nov 25, 2019 at 17:30
  • 2
    Also your object-type properties are all missing Set in the bodies of the methods. Commented Nov 25, 2019 at 18:12
  • Thanks all for your answers! I'm a complete novice when it comes to using Types and Class Modules so your feedback is really helpful. @Sorceri thanks. Do I need that when I've got the elements in the type? @MathieuGuindon thanks. Would you suggest I declare my types as Private in the class modules instead? Or how to avoid exposing public UDTs? Also my clsMod already has a Public Property Get clsTes2() as clsMo2, am I doing something else wrong? I shall drop the prefixing, apologies for any damage to your eyes! @TimWilliams thanks. Does having Set clstest..etc in Variables sub not do this? Commented Nov 26, 2019 at 10:07

1 Answer 1

1

You might be over-complicating things. Try with a simpler example first.

See if you can follow the skeleton code provided below

Class1

Option Explicit

Private m_name As String

Private Sub Class_Initialize()
    m_name = "Default"
End Sub

Public Property Get Name() As String
    Name = m_name
End Property

Public Property Let Name(ByVal x as string)
    m_name = x
End Property

Class2

Option Explicit

Private m_info As Class1

Public Property Get Info() As Class1
    Set Info = m_info
End Property

Public Property Set Info(ByRef X As Class1)
    Set m_info = X
End Property

Private Sub Class_Initialize()
    Set m_info = New Class1
End Sub

Module

Option Explicit

Public Sub TestCode()
    
    Dim t As New Class2
    Debug.Print t.Info.Name
    ' Default

    t.Info.Name = "Another"
 
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.