gpt4 book ai didi

excel - 使用类模块在类型内输入

转载 作者:行者123 更新时间:2023-12-04 20:26:35 30 4
gpt4 key购买 nike

我正在尝试使用类模块在类型中构造一个类型。

我有一个带有我的类型的标准代码模块,以及一个带有每种类型的 Set/Let/Get 属性的类模块。然后,我举了一个例子来说明我是如何尝试使用它们的。

此方法适用于一种类型,但不适用于类型中的类型。在标准模块中设置第二种类型时,我得到一个

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



类模块 1,称为 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

类模块 2,称为 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

带有类型的标准代码模块
Type MyTyp2
stri As String
End Type

Type MyType
m_clsTes2 As MyTyp2
End Type

带有 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

最佳答案

你可能把事情复杂化了。首先尝试一个更简单的示例。
看看能不能按照下面提供的骨架代码
第一类

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
2 类
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
模块
Option Explicit

Public Sub TestCode()

Dim t As New Class2
Debug.Print t.Info.Name
' Default

t.Info.Name = "Another"

End Sub

关于excel - 使用类模块在类型内输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59036908/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com