VBA - 尝试实现继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11843940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA - trying to implement inheritance
提问by vkrams
I am trying to implement Inheritance in VBA in the following way -
我正在尝试通过以下方式在 VBA 中实现继承 -
I have one class module clsRange
as shown below
我有一个类模块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
Another class module clsChildRange
另一个类模块 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
I have a module, In that I am trying to create an object for clsChildRange and try to set the properties of clsRange in the following way
我有一个模块,我正在尝试为 clsChildRange 创建一个对象,并尝试按以下方式设置 clsRange 的属性
Dim objCRng as New clsChildRange
objCRng.RangeName= "Range1"
But I get an error - object variable or with block variable not set.
但我收到一个错误 - 对象变量或未设置块变量。
回答by Dick Kusleika
As Uri said, rngHolder is not instantiated which is causing the problem. Don't test for Null, but test of Is Nothing. Here are two ways to do this, depending on what you're trying to accomplish.
正如 Uri 所说,rngHolder 没有被实例化,这导致了问题。不要测试 Null,但测试 Is Nothing。这里有两种方法可以做到这一点,具体取决于您要完成的任务。
Explicitly Set the Range
显式设置范围
In CRange
在 CRange
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
In CChildRange
在 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
Then in a standard module
然后在标准模块中
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
Implicitly set the Range
隐式设置范围
CRange is the same.
CRange 是一样的。
In CChildRange
在 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
Then in a standard module
然后在标准模块中
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
回答by Uri Goren
I suspect that the rngHolder member isn't getting initialized in this matter, try this code
我怀疑 rngHolder 成员在这件事上没有被初始化,试试这个代码
private rngHolder as clsRange
Public Property Get RangeName() As String
If rngHolder=Null Then Set rngHolder=New clsRange
Set RangeName = rngHolder.RangeName
End Property
Public Property Let RangeName(ByVal thisRangeName As String)
If rngHolder=Null Then Set rngHolder=New clsRange
rngHolder.RangeName = thisRangeName
End Property