gpt4 book ai didi

VBA - 试图实现继承

转载 作者:行者123 更新时间:2023-12-02 07:40:15 24 4
gpt4 key购买 nike

我正在尝试通过以下方式在 VBA 中实现继承 -

我有一个类模块 clsRange 如下所示

Private strRngName as String

Public Property Let RangeName(ByVal thisRangeName As String)
strRngName = thisRangeName
End Property

Public Property Get RangeName() As String
RangeName= strRngName
End Property

另一个类模块clsChildRange

private rngHolder as New clsRange

Public Property Get RangeName() As String
Set RangeName = rngHolder.RangeName
End Property

Public Property Let RangeName(ByVal thisRangeName As String)
rngHolder.RangeName = thisRangeName
End Property

我有一个模块,因为我正在尝试为 clsChildRange 创建一个对象并尝试以下列方式设置 clsRange 的属性

Dim objCRng  as New clsChildRange

objCRng.RangeName= "Range1"

但我得到一个错误 - 对象变量或 block 变量未设置。

最佳答案

正如 Uri 所说,rngHolder 没有被实例化,这是导致问题的原因。不要测试 Null,而是测试 Is Nothing。这里有两种方法可以做到这一点,具体取决于您要实现的目标。

明确设置范围

在 C 范围内

Private msRngName As String

Public Property Let RngName(ByVal sRngName As String): msRngName = sRngName: End Property
Public Property Get RngName() As String: RngName = msRngName: End Property

在 CChildRange 中

Private mclsRange As CRange

Public Property Set Range(ByVal clsRange As CRange): Set mclsRange = clsRange: End Property
Public Property Get Range() As CRange: Set Range = mclsRange: End Property

Public Property Get RngName() As String

If Not Me.Range Is Nothing Then
RngName = Me.Range.RngName
End If

End Property

Public Property Let RngName(sName As String)

If Not Me.Range Is Nothing Then
Me.Range.RngName = sName
End If

End Property

然后在标准模块中

Sub test()

Dim clsRange As CRange
Dim clsChildRange As CChildRange

'Create a new CRange instance
Set clsRange = New CRange

'Create a new CChildRange instance
Set clsChildRange = New CChildRange

'Set the Range property to the CRange instance
Set clsChildRange.Range = clsRange

'Set the RngName property of the chile
clsChildRange.RngName = "Range1"

'Test that the parent has the property set
Debug.Assert clsRange.RngName = "Range1"

End Sub

隐式设置范围

CRange 是一样的。

在 CChildRange 中

Private mclsRange As CRange

Public Property Set Range(ByVal clsRange As CRange): Set mclsRange = clsRange: End Property
Public Property Get Range() As CRange: Set Range = mclsRange: End Property

Public Property Get RngName() As String

RngName = Me.Range.RngName

End Property

Public Property Let RngName(sName As String)

Me.Range.RngName = sName

End Property

Private Sub Class_Initialize()

Set mclsRange = New CRange

End Sub

Private Sub Class_Terminate()

Set mclsRange = Nothing

End Sub

然后在标准模块中

Sub test()

Dim clsChildRange As CChildRange

'Create a new CChildRange instance
'Range object created when class is created
Set clsChildRange = New CChildRange

'Set the RngName property of the chile
clsChildRange.RngName = "Range1"

'Test that the parent has the property set
Debug.Assert clsChildRange.Range.RngName = "Range1"


End Sub

关于VBA - 试图实现继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11843940/

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