首页 > 解决方案 > Debug.Print 在 VBA Step 与 Run 中的触发方式不同。为什么?

问题描述

我有一个接口IFoo和一个Foo实现IFoo. FooPredelaredID = True

IFoo:
Public Property Get Name() As String
End Property
Public Property Get Number() As Long
End Property


Foo:
Implements IFoo
Private Type TFoo
    Name As String
    Number As Long
End Type
Private this As TFoo

Private Sub Class_Initialize()
    Debug.Print "Initializing " & TypeName(Me)
End Sub
Private Sub Class_Terminate()
    Debug.Print "Terminating " & TypeName(Me)
End Sub

Private Property Get IFoo_Name() As String
    IFoo_Name = this.Name
End Property
Private Property Get IFoo_Number() As Long
    IFoo_Number = this.Number
End Property
Private Sub DoFoo(ByVal Name As String, ByVal Number As Long)
    Debug.Print "DoingFoo", Name, Number
End Sub

'Factory
Public Function Create(ByVal Name As String, Number As Long) As IFoo
    With New Foo
        .Name = Name
        .Number = Number
        Set Create = .Self
        DoFoo Name, Number
    End With
End Function
Public Property Get Self() As IFoo
    Set Self = Me
End Property
Public Property Let Name(ByVal RHS As String)
    this.Name = RHS
End Property
Public Property Let Number(ByVal RHS As Long)
    this.Number = RHS
End Property

调用代码:

Public Sub Main()
    Dim myFoo As IFoo
    With Foo
        Set myFoo = .Create("myFooName", 42)
    End With
    Set myFoo = Nothing
End Sub

当我运行时,Main我进入即时窗格:

Initializing Foo
Initializing Foo
DoingFoo      myFooName      42 
Terminating Foo

当我通过 (F8) 时,我得到:

Initializing Foo
DoingFoo      myFooName      42 
Terminating Foo

为什么我在使用 Run 时会得到额外的“Initializing Foo”?

标签: excelvba

解决方案


推荐阅读