自定义事件未触发 [英] Custom Event not firing

查看:124
本文介绍了自定义事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel 2010中,我似乎在使用自定义事件变得奇怪而又意想不到的行为。



我喜欢99%的确定这种方法在几年前对我有用(也许是在Excel 03/07 - 不记得了) / em> 也许我刚刚搞砸了一些东西 ...



p>

添加一个新的类模块并将其命名为工厂

 公共事件AfterInitialize()

Private Sub Class_Initialize()
RaiseEvent AfterInitialize
End Sub

添加另一个类模块,名称为 FactoryTest

  Private WithEvents cFactory As Factory 

Private Sub Class_Initialize()
Set cFactory = New Factory
End Sub

Private Sub cFactory_AfterInitialize()
Debug.Printin alcedized ...
End Sub

和标准 Module1 并运行以下

  Sub Main()

Dim fTest As FactoryTest
设置fTest = New FactoryTest

End Sub

在这一点上我预计在立即窗口中初始化后看到 ,但我不...



代码似乎从未达到 Private Sub cFactory_AfterInitialize() ...



注意:



我可以添加一个公共子: RaiseAfterInitialize()工厂类,然后在 Initialize()事件在 FactoryTest (如 cFactory)中显式调用。 RaiseAfterInitialize()可能是一个可能的工作,但我真正想了解的是为什么它不起作用上面显示的原始方式?



MSDN上的VBA活动没有太多



任何人都可以指出我做错了什么?

解决方案

根据VBA语言规范部分 5.3.1.10生命周期处理程序声明,我猜这是原因(强调我的):


如果一个类定义了一个Class_Initialize生命周期处理程序,该子程序将作为一种方法被调用,每当该类的实例由New运算符创建时,通过引用一个变量,该变量被声明为一个< as-auto-object> 和其当前值为Nothing,或通过调用VBA标准库的CreateObject函数(第6.1.2.8.1.4节)。调用的目标对象是新创建的对象。


所以在你的情况下,在行



设置cFactory =新工厂



Class_Initialize 方法 Factory 之前运行这意味着在事件发生时, FactoryTest 类实例不知道。






更新



我通过添加一个方法给了它一个快速测试工厂调用 Class_Initialize 函数:

  public Sub test()
Class_Initialize
End Sub

然后添加一个调用它作为 FactoryTest.Class_Initialize 方法的一部分:

 私人Sub Class_Initialize()
设置cFactory = New Factory
cFactory.test
End Sub

由于调用方法 test 发生在新工厂已分配给 cFactory 之后, after initialized ...消息按预期显示。


I am in Excel 2010 and I seem to be getting a strange rather unexpected behaviour working with custom events.

I am like 99% sure this approach has worked for me a few years ago (maybe it was in Excel 03/07 - can't remember) or maybe I've just messed something up...

Here's a repro:

Add a new class module and name it Factory

Public Event AfterInitialize()

Private Sub Class_Initialize()
    RaiseEvent AfterInitialize
End Sub

Add another class module and name is FactoryTest

Private WithEvents cFactory As Factory

Private Sub Class_Initialize()
    Set cFactory = New Factory
End Sub

Private Sub cFactory_AfterInitialize()
    Debug.Print "after inialized..."
End Sub

and a standard Module1 and run the below

Sub Main()

    Dim fTest As FactoryTest
    Set fTest = New FactoryTest

End Sub

At this point I expected to see after initialized.. in the Immediate Window but I don't...

Stepping-through the code it seems that the Private Sub cFactory_AfterInitialize() is never reached...

Note:

I can add a public sub: RaiseAfterInitialize() to the Factory class and then call that explicitly in the Initialize() event in the FactoryTest like cFactory.RaiseAfterInitialize() so that could be a possible work around but what I am really trying to understand is why it doesn't work the original way showed above?

There isn't much on VBA events on MSDN

Can anyone point out what I am doing wrong?

解决方案

Based on the VBA Language Specification section 5.3.1.10 Lifecycle Handler Declarations, I would guess this is the reason (emphasis mine):

If a class defines a Class_Initialize lifecycle handler, that subroutine will be invoked as an method each time an instance of that class is created by the New operator, by referencing a variable that was declared with an <as-auto-object> and whose current value is Nothing, or by call the CreateObject function (section 6.1.2.8.1.4) of the VBA Standard Library. The target object of the invocation is the newly created object. The invocation occurs before a reference to the newly created object is returned from the operations that creates it.

So in your case, in the line

Set cFactory = New Factory

The Class_Initialize method of Factory is run before the assignment is made, which means while the event IS raised, the FactoryTest class instance doesn't know about it.


UPDATE

I gave it a quick test by adding a method to Factory which calls the Class_Initialize function:

Public Sub test()
    Class_Initialize
End Sub

And then added a call to it as part of the FactoryTest.Class_Initialize method:

Private Sub Class_Initialize()
    Set cFactory = New Factory
    cFactory.test
End Sub

Since the call to the method test takes place after the New Factory has been assigned to cFactory, the "after initialized..." message displays as expected.

这篇关于自定义事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆